Professional Documents
Culture Documents
BARRACKPORE
700120
COMPUTER PRACTICAL
(C++ PROGRAMS, SQL QUERIES)
2
1. Program to print Armstrong numbers from 1
to 500
2. Program to find the largest number among
three numbers
3. Program to input a number and check whether
the number is perfect or not
4. Program to print the prime Fibonacci series
from 1 to 1000
5. Program to print prime factors of a number
6. Program to input a number and check whether
the number is perfect or not
7. Program to print sum of series 1+1/12+1/32+....
....1/n2
8. Program to print Pascal’s triangle
9. Program to create a string and display the
character from even, odd and prime positions
10. Program to store the information of 5
employees and to display the information of
employee depending on the employee number
(Structure based)
11. Menu-driven program to perform various
operations on a string without using built in
functions- i)Find length ii)Reverse string
iii)Copy the string iv)Concatenate two strings
12. Menu-driven program to accept an array and
sort using: i)Selection Sort ii)Insertion Sort
13. Program to create Magic Square of order nxn.
(In such a box, the sum of elements of any row
or column is the same)
14. Write a program to read an integer array, sort
it using bubble sort and search an element
using binary search
15. Write a program to implement merge sort
16. Write a program to insert an element at a
Specific position
17. Write a program to delete an element from a
specific position in a 1-D array
3
18. Write a program to display the country and
corresponding capital
19. Write a program to accept a 2-D array of
integers and display the 2-digit numbers
20. Write a program to multiply two matrices
21. Write a program to create a text file to read
and display uppercase and lowercase and
consonants
22. Write a program to implement various file
operations
23. Write a program to create library management
system
24. Write a program to create linear stack of
integers and implement push and pop
operations
25. Write a program to create linear circular queue
and to perform addition and deletion on it
26. Write a program to implement push and pop
on a stack of array
27. Write a program to create a linked queue
containing employee number, salary and to
perform addition and deletion
28. Write a program to create linear circular queue
and perform addition on it
29. Write a program to create linear stack
containing roll number and age and perform
push and pop on it
30. SQL Queries
PROGRAM 1
4
Program to print Armstrong numbers from 1 to 500
#include<iostream.h>
void main()
int a,d,c=0;
for(int i=1;i<=500;i++)
a=i; c=0;
while(a!=0)
d=a%10;
c=c+(d*d*d);
a=a/10;
if(c==i)
cout<<i<<endl;
5
6
PROGRAM 2
Program to find the largest number among three
numbers
#include<iostream.h>
void main()
int a,b,c,h;
cin>>a>>b>>c;
h=a;
if(b>h)
h=b;
if(c>h)
h=c;
7
8
PROGRAM 3
Program to input a number and check whether the
number is perfect or not
#include<iostream.h>
void main()
int n,i=1,sum=0;
cout<<"Enter a number:";
cin>>n;
for(i=1;i<n;i++)
if(n%i==0)
sum+=i;
if(sum==n)
else
9
10
PROGRAM 4
Program to print the prime Fibonacci series from 1
to 1000
#include<iostream.h>
void main()
int i,c=0,first=1,second=1,next;
cout<<first<<endl<<second<<endl;
for(i=2;i<=15;i++)
next=first+second;
for(i=1;i<=next;i++)
if(next%i==0)
c++;
if(c==2)
cout<<next<<endl;
first=second;
second=next;
11
}
12
PROGRAM 5
Program to print prime factors of a number
#include<iostream.h>
void main()
int n,i,j,c=0;
cin>>n;
for(i=1;i<=n;i++)
{ c=0;
if(n%i==0)
for(j=1;j<=i;j++)
{if(i%j==0)
{c++;}}
if(c==2)
cout<<i<<endl;
13
14
PROGRAM 6
Program to input a number and check whether the
number is perfect or no
#include<iostream.h>
void main()
int n,d,c=0;
cin>>n;
while(n!=0)
d=n%10;
c=c+d;
n=n/10;
if(c==1)
break;
if(n==0)
{n=c; c=0;}
15
}
16
PROGRAM 7
Program to print sum of series
1+1/1^2+1/3^2+........1/n^2
#include<iostream.h>
void main()
cin>>n;
for(int i=1;i<=n;i++)
if(i%2!=0)
s=s+(1/(i*i));
17
18
PROGRAM 8
Program to print Pascal’s triangle
#include<iostream.h>
void main()
int n,k,i,x;
cin>>n;
for(i=0;i<=n;i++)
x=1;
for(k=0;k<=i;k++)
cout<<x<<" ";
x=x*(i-k)/(k+1);
cout<<endl;
19
20
PROGRAM 9
Program to create a string and display the
character from even, odd and prime positions
#include<iostream.h>
#include<stdio.h>
#include<string.h>
void main()
for(i=0;i<l;i++)
if((i+1)%2==0)
cout<<"Pos "<<(i+1)<<":"<<s[i]<<endl;
for(i=0;i<l;i++)
21
if((i+1)%2!=0)
cout<<"Pos "<<(i+1)<<":"<<s[i]<<endl;
for(i=0;i<l;i++)
c=0;
for(j=1;j<=(i+1);j++)
if((i+1)%j==0)
c++;
if(c==2)
cout<<"Pos "<<(i+1)<<":"<<s[i]<<endl;
22
PROGRAM 10
Program to store the information of 5 employees
and to display the information of employee
depending on the employee number (Structure
based)
#include<iostream.h>
23
#include<string.h>
#include<stdio.h>
struct emp{
int empno;
char name[100];
char desig[50];
};
emp ob[5];
void main()
int i,n,p;
for(i=0;i<5;i++)
cin>>n;
for(i=0;i<5;i++)
if(ob[i].empno==n)
p=i;
24
break;}
25
PROGRAM 11
Menu-driven program to perform various
operations on a string without using built in
functions- i)Find length ii)Reverse string iii)Copy
the string iv)Concatenate two strings
#include<iostream.h>
#include<math.h>
#include<stdio.h>
int i,k;
void main()
26
cout<<"Enter the string in 100 characters:"; gets(s);
cout<<"Enter 2 to reverse"<<endl;
switch(ch)
case 1: l=slen(s);
break;
case 2: srev(s,a);
break;
case 3: scopy(s,a);
puts(a);
break;
gets(q);
sconcat(s,q,r);
27
break;
for(i=0;v[i]!='\0';i++);
return i;
for(int j=slen(d)-1,k=0;j>=0;j--,k++)
f[k]=d[j];
f[k]='\0';
for(int z=0;z<slen(h);z++)
t[z]=h[z];
t[z]='\0';
28
}
for(int q=0;k[q]!='\0';q++)
p[q]=k[q];
for(int e=0;l[e]!='\0';e++)
p[q+e]=l[e];
p[q+e]='\0';
29
PROGRAM 12
Menu-driven program to accept an array and sort
using: i)Selection Sort ii)Insertion Sort
#include<iostream.h>
void main()
int ch,i,ar[10];
for(i=0;i<10;i++)
cin>>ar[i];
30
cin>>ch;
switch(ch)
selc(ar);
for(i=0;i<10;i++)
cout<<ar[i]<<endl;
break;
insr(ar);
for(i=0;i<10;i++)
cout<<ar[i]<<endl;
int i,j,small,pos,temp;
for(i=0;i<10;i++)
31
small=num[i];
pos=i;
for(j=i+1;j<10;j++)
if(num[j]<small)
{small=num[j]; pos=j;}
temp=num[i];
num[i]=num[pos];
num[pos]=temp;
} }
int i,j,key;
for(j=1;j<10;j++)
{ key=num[j];
for(i=9;(i>=0)&&(num[i]<key);i--)
num[i+1]=num[i];
num[i+1] = key;
}}
32
33
PROGRAM 13
Program to create Magic Square of order nxn.
(In such a box, the sum of elements of any row or
column is the same)
#include<iostream.h>
#include<iomanip.h>
void main()
int MagicSquare[n][n];
int newRow,
newCol;
int i=0 ;
int j=n/2;
34
//Fill each element of the array using the magic array
MagicSquare[i][j]=value;
newRow=(i+1)%n;
newCol=(j+1)%n;
// next assignment.
if(MagicSquare[newRow][newCol]==0)
i=newRow;
j=newCol;
else
// The cell was full. Use the cell above the previous one.
i=(i-1+n)%n;
for(int x=0;x<n;x++)
35
{
for(int y=0;y<n;y++)
cout<<MagicSquare[x][y]<<" ";
cout<<endl;
}}
36
PROGRAM 14
Write a program to read an integer array, sort it
using bubble sort and search an element using
binary search
#include<iostream.h>
void main()
int n,a[50],p,it;
for(int i=0;i<n;i++)
cin>>a[i];
Sort(a,n);
for(int z=0;z<n;z++)
cout<<a[z]<<" ";
p=bi(a,n,it);
if(p==-1)
cout<<"Not found!!";
37
else
{int tmp,ctr=0;
for(int i=0;i<size;i++)
{ for(int j=0;j<(size-1);j++)
{ if(AR[j]>AR[j+1])
{ tmp=AR[j];
AR[j]=AR[j+1];
AR[j+1]=tmp;
{ int beg,last,mid;
beg=0; last=size-1;
while(beg<=last)
{ mid=(beg+last)/2;
if(item==AR[mid])
return mid;
else if(item>AR[mid])
38
beg=mid+1;
else if(item<AR[mid])
last=mid-1;
else
break;
return -1;
39
PROGRAM 15
Write a program to implement merge sort
#include <iostream.h>
#include <conio.h>
#define MAX 10
class mergesort{
int arr1[MAX],arr2[MAX],n1,n2,n3;
public:
void getdata();
void showdata(int);
void mergeLogic();
void sortLogic();
};
int i;
cin>>n1;
40
for(i=0;i<n1;i++)
cin>>arr1[i];
cin>>n2;
for(i=0;i<n2;i++)
cin>>arr2[i];
n3=n1+n2;
int i;
if(select==1){
cout<<"\n\n--Array 1--\n";
for(i=0;i<n1;i++)
cout<<arr1[i]<<" ";
else if(select==2){
cout<<"\n\n--Array 2--\n";
for(i=0;i<n2;i++)
cout<<arr2[i]<<" ";
else if(select==3){
cout<<"\n\n--Sorted Array--\n";
for(i=0;i<n3;i++)
41
cout<<sort_arr[i]<<" ";
int i,j,c;
for(i=0;i<n1;i++)
merge_arr[i] = arr1[i];
for(j=i,c=0;j<n3;j++,c++)
merge_arr[j] = arr2[c];
mergeLogic();
int i,j,first=0,second=n1,third=n3;
i=first;
j=second;
42
while(i<second && j<third){
sort_arr[c]=merge_arr[i];
i++;
else{
sort_arr[c]=merge_arr[j];
j++;
c++;
if(i<second){
while(i<second){
sort_arr[c]=merge_arr[i];
i++;
c++;
if(j<third){
while(j<third){
sort_arr[c]=merge_arr[j];
j++;
43
c++;
clrscr();
cout<<"\n*****Merge Sort*****\n";
mergesort obj;
obj.getdata();
obj.sortLogic();
obj.showdata(1);
obj.showdata(2);
obj.showdata(3);
getch();
44
45
PROGRAM 16
Write a program to insert an element at a Specific
position
#include<iostream.h>
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
scanf("%d", &position);
scanf("%d", &value);
46
for (c = n - 1; c >= position - 1; c--)
array[c+1] = array[c];
array[position-1] = value;
printf("%d\n", array[c]);
return 0;
PROGRAM 17
47
Write a program to delete an element from a
specific position in a 1-D array
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
scanf("%d", &position);
else
array[c] = array[c+1];
48
printf("Resultant array is\n");
printf("%d\n", array[c]);
return 0;
PROGRAM 18
49
Write a program to display the country and
corresponding capital
#include<iostream.h>
#include<stdio.h>
#include<string.h>
struct ca{
char co[20];
char cap[20];
};
void main()
ca stor[5];
for(i=0;i<5;i++)
gets(cn);
cout<<"COUNTRY"<<" "<<"CAPITAL"<<endl;
50
for(i=0;i<5;i++)
{ if(strcmp(cn,stor[i].co)==0)
{ cout.write(stor[i].co,strlen(stor[i].co));
cout<<" ";
cout.write(stor[i].cap,strlen(stor[i].cap));
PROGRAM 19
51
Write a program to accept a 2-D array of integers
and display the 2-digit numbers
#include<iostream.h>
void main()
int ar[10][10],r,c,i,j,s=0,d;
cout<<endl;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>ar[i][j];
for(i=0;i<r;i++)
cout<<endl;
for(j=0;j<c;j++)
cout<<ar[i][j]<<" ";
for(i=0;i<r;i++)
52
{
for(j=0;j<c;j++)
while(n!=0)
d=n%10;
s++;
n=n/10;
if(s==2)
cout<<ar[i][j]<<",";
53
54
PROGRAM 20
Write a program to multiply two matrices
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int a[3][3],b[3][3],i,j,k,s;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>b[i][j];
55
}
cout<<"Multiplication is"<<endl;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
s=s+a[i][k]*b[k][j];
cout<<s<<" ";
s=0;
cout<<endl;
getch();
56
57
PROGRAM 21
Write a program to create a text file to read and
display uppercase and lowercase and consonants
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
void main()
ofstream fout;
fout.open("Aschars",ios::app);
if(!fout)
for(int i=33;i<128;i++)
fout.put(((char)i));
fout.close();
ifstream fin;
fin.open("Aschars",ios::in);
58
fin.seekg(0);
for(i=33;i<128;i++)
fin.get(ch);
cout<<"Uppercase"<<endl;
for(i=65;i<122;i++)
if(i>=65&&i<=90)
{cout<<" "<<i<<"=";
cout.put((char)i);
if(!(i%8))
{cout<<endl; line++;}}
cout<<endl<<"Lowercase"<<endl;
for(i=65;i<122;i++)
if(i>=97&&i<=122)
{cout<<" "<<i<<"=";
cout.put((char)i);
if(!(i%8))
{cout<<endl; line++;}}
cout<<endl<<"Vowels"<endl;
for(i=65;i<122;i++)
59
{
if(char(i)=='a'||char(i)=='A'||char(i)=='e'||char(i)=='E'||char(i)=='I'
||char(i)=='i'||char(i)=='O'||char(i)=='o'||char(i)=='u'
||char(i)=='U')
{cout<<" "<<i<<"=";
cout.put((char)i);
if(!(i%8))
{cout<<endl; line++;}}
} }
60
PROGRAM 22
Write a program to implement various file
operations
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
void main()
int choice;
while(1)
clrscr();
cout<<"5) Exit\n";
cin>>choice;
switch (choice)
61
{
case 1 : {
ofstream outfile;
outfile.open("emp",ios::out);
char name[20];
cin>>name;
outfile<<name<<endl;
int age;
cin>>age;
outfile<<age<<endl;
char lang[25];
cin>>lang;
outfile<<lang<<endl;
totrec= totrec + 1;
outfile.close();
break;
case 2 : {
ofstream outfile;
outfile.open("emp",ios::app);
62
cout<<"\n\nPlease enter the details as per demanded\n";
char name[20];
cin>>name;
outfile<<name<<endl;
int age;
cin>>age;
outfile<<age<<endl;
char lang[25];
cin>>lang;
outfile<<lang<<endl;
totrec = totrec + 1;
outfile.close();
break;
case 3 : {
ofstream outfile;
outfile.open("emp",ios::ate);
char ans;
cin>>ans;
if(ans=='y' || ans=='Y')
63
{
char name[20];
cin>>name;
outfile<<name<<endl;
int age;
cin>>age;
outfile<<age<<endl;
char lang[25];
cin>>lang;
outfile<<lang<<endl;
totrec = totrec + 1;
outfile.close();
break;
case 4 : {
ifstream infile;
infile.open("emp",ios::in);
char line[size];
64
int counter=totrec;
while(counter > 0)
infile.getline(line,size);
cout<<"\n\nNAME : "<<line<<endl;
infile.getline(line,size);
cout<<"AGE : "<<line<<endl;
infile.getline(line,size);
cout<<"LANGUAGE : "<<line<<endl;
counter--;
infile.close();
getch();
break;
out:
65
66
PROGRAM 23
Write a program to create library management
system
#include <iostream.h>
#include <iomanip.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
class library
char author[50],title[50];
int price[15];
char pub[50];
int s;
public:
void getdata(void);
void display(void);
};
library ord[size];
67
void library :: getdata(void)
cin>>s;
for(int i=0;i<s;i++)
gets(ord[i].author);
gets(ord[i].title);
cin>>ord[i].price[i];
gets(ord[i].pub);
cout<<setw(50)<<"LIBRARY DATABASE";
cout<<endl<<endl;
for(int a=0;a<40;a++)
cout<<"*-";
68
cout<<endl;
cout<<"AUTHOR NAME"<<setw(20)<<"BOOK
TITLE"<<setw(22)<<"PRICE"<<setw(18)<<"PUBLISHER"<<endl;
for(int b=0;b<40;b++)
cout<<"*-";
cout<<endl;
for(int i=0;i<s;i++)
cout.write(ord[i].author,strlen(ord[i].author));
cout<<" ";
cout.write(ord[i].title,strlen(ord[i].title));
cout.write(ord[i].pub,strlen(ord[i].pub))<<endl;
void main()
library o1;
o1.getdata();
o1.display();
getch();
69
70
PROGRAM 24
Write a program to create linear stack of integers
and implement push and pop operations
#include<iostream.h>
#include<stdlib.h>
#include<process.h>
Node *next;
}*top,*newptr,*save,*ptr;
Node *Create(int);
void Push(Node*);
void Display(Node*);
void Pop();
int main()
top=NULL;
while(ch=='y'||ch=='Y')
cin>>inf;
newptr=Create(inf);
if(newptr==NULL)
71
{ cout<<"\nCannot create new node!!! Aborting!!\n";
system("pause");
exit(1);
Push(newptr);
cin>>ch;
system("cls");
do
Display(top); system("pause");
cin>>ch;
if(ch=='y'||ch=='Y')
Pop();
}while(ch=='y'||ch=='Y');
return 0;
Node *Create(int n)
{ptr=new Node;
ptr->info=n;
ptr->next=NULL;
72
return ptr;
{ if(top==NULL)
top=np;
else
{ save=top;
top=np;
np->next=save;
void Pop()
{ if(top==NULL)
cout<<"UNDERFLOW!!\n";
else
{ ptr=top; top=top->next;
delete ptr;
while(np!=NULL)
{ cout<<np->info<<"->";
73
np=np->next;
cout<<"!!!\n";
OUTPUT:
9->8->6->!!!
8->6->!!!
6->!!!
74
PROGRAM 25
Write a program to create linear circular queue and
to perform addition and deletion on it
#include<iostream.h>
#include<stdlib.h>
#include<process.h>
int Insert_in_CQ(int[],int);
void Display(int[],int,int);
int CQueue[size],front=-1,rear=-1;
int main()
{int Item,res,ch;
do{ system("cls");
cout<<"\t1.Insert\n";
cout<<"\t2.Delete\n";
cout<<"\t3.Display\n";
cout<<"\t4.Exit\n";
cin>>ch;
switch(ch)
75
{ case 1:cout<<"\nEnter item for insertion: ";
cin>>Item;
res=Insert_in_CQ(CQueue,Item);
if(res==-1) cout<<"Overflow!!\n";
else
Display(CQueue,front,rear);
system("pause");
break;
case 2:Item=Del_in_CQ(CQueue);
Display(CQueue,front,rear);
system("pause");
break;
case 3:Display(CQueue,front,rear);
system("pause");
break;
case 4:break;
default:cout<<"Inavlid input..\n";
system("pause");
break;
}while(ch!=4);
76
return 0;
if((front==0&&rear==size-1)||(front==rear+1))
return -1;
else rear++;
CQueue[rear]=ele;
return 0;
{int i=0;
if(front==-1) return;
if(rear>=front)
{ for(i=0;i<front;i++) cout<<"-";
cout<<">>>";
for(i=front;i<rear;i++) cout<<CQueue[i]<<"<-";
cout<<CQueue[rear]<<"<<<"<<endl;
77
else
{ for(i=0;i<rear;i++) cout<<CQueue[i]<<"<-";
cout<<CQueue[rear]<<"<<<";
for(;i<front;i++) cout<<"-";
cout<<">>>";
for(i=front;i<size;i++) cout<<CQueue[i]<<"<-";
cout<<"\t...wrap around...";
{ int ret;
else
{ ret=CQueue[front];
if(front==rear) front=rear=-1;
else if(front==size-1)
front=0;
else front++;
return ret;
78
OUTPUT:
1. Insert
2. Delete
3. Display
4. Exit
Cir_Queue is:
>>>3<-5<-8<-9<<<
1. Insert
2. Delete
3. Display
4. Exit
Cir_Queue is:
>>>5<-8<-9<<<
1. Insert
2. Delete
3. Display
79
4. Exit
80
PROGRAM 26
Write a program to implement push and pop on a
stack of array
#include<iostream.h>
#include<process.h>
int Pop(int[],int&);
int Push(int[],int&,int);
void Display(int[],int);
int main()
{int Stack[size],Item,top=-1,res;
char ch='y';
while(ch=='y'||ch=='Y')
cin>>Item;
res=Push(Stack,top,Item);
if(res==-1)
{ cout<<"Overflow!! Aborting!!\n";
exit(1);
Display(Stack,top);
81
cin>>ch;
ch='y';
while(ch=='y'||ch=='Y')
{ res=Pop(Stack,top);
if(res==-1)
{ cout<<"Underflow!! Aborting!!\n";
exit(1);
else
Display(Stack,top);
cin>>ch;
return 0;
else
{ top++;
82
Stack[top]=ele;
return 0;
{int ret;
else
{ ret=Stack[top];
top--;
return ret;
{if(top==-1)return;
cout<<Stack[top]<<"<--"<<endl;
for(int i=top-1;i>=0;i--)
cout<<Stack[i]<<endl;
83
84
PROGRAM 27
Write a program to create a linked queue containing
employee number, salary and to perform addition
and deletion
#include<iostream.h>
#include<stdlib.h>
#include<process.h>
int sal;
emp *next;
}*front,*newptr,*save,*ptr,*rear;
emp *Create(int,int);
void Insert(emp*);
void Display(emp*);
void DelNode_Q();
int main()
front=rear=NULL;
while(ch=='y'||ch=='Y')
85
newptr=Create(num,sa);
if(newptr==NULL)
exit(1);
Insert(newptr);
cin>>ch;
system("cls");
do
Display(front);
cin>>ch;
if(ch=='y'||ch=='Y')
DelNode_Q();
}while(ch=='y'||ch=='Y');
return 0;
{ptr=new emp;
ptr->empno=n;
86
ptr->sal=m;
ptr->next=NULL;
return ptr;
{if(front==NULL) front=rear=np;
else
{ rear->next=np; rear=np;}
void DelNode_Q()
{ if(front==NULL) cout<<"Underflow!!\n";
else
{ ptr=front;
front=front->next;
delete ptr;
{while(np!=NULL)
{ cout<<”\n(Number,Salary)”<<np->empno<<”,”<<np->sal<<”->”;
87
np=np->next;
}cout<<”!!!\n”;
OUTPUT:
(Number,Salary)->756,50000->312,80000->!!!
(Number,Salary)->312,80000->!!!
88
PROGRAM 28
Write a program to create linear circular queue and
perform addition on it
#include<iostream.h>
#include<stdlib.h>
#include<process.h>
int Insert_in_CQ(int[],int);
void Display(int[],int,int);
int CQueue[size],front=-1,rear=-1;
int main()
{int Item,res,ch;
do{ system("cls");
cout<<"\t1.Insert\n";
cout<<"\t2.Display\n";
cout<<"\t3.Exit\n";
cin>>ch;
switch(ch)
89
cin>>Item;
res=Insert_in_CQ(CQueue,Item);
if(res==-1) cout<<"Overflow!!\n";
else
Display(CQueue,front,rear);
system("pause");
break;
case 2:Display(CQueue,front,rear);
system("pause");
break;
case 3:break;
default:cout<<"Inavlid input..\n";
system("pause");
break;
}while(ch!=4);
return 0;
if((front==0&&rear==size-1)||(front==rear+1))
90
return -1;
else rear++;
CQueue[rear]=ele;
return 0;
{int i=0;
if(front==-1) return;
if(rear>=front)
{ for(i=0;i<front;i++) cout<<"-";
cout<<">>>";
for(i=front;i<rear;i++) cout<<CQueue[i]<<"<-";
cout<<CQueue[rear]<<"<<<"<<endl;
else
{ for(i=0;i<rear;i++) cout<<CQueue[i]<<"<-";
cout<<CQueue[rear]<<"<<<";
for(;i<front;i++) cout<<"-";
cout<<">>>";
for(i=front;i<size;i++) cout<<CQueue[i]<<"<-";
91
cout<<"\t...wrap around...";
OUTPUT:
1. Insert
2. Display
3. Exit
Cir_Queue is:
>>>3<-5<-8<-9<<<
1. Insert
2. Delete
3. Display
4. Exit
92
PROGRAM 29
Write a program to create linear stack containing
roll number and age and perform push and pop on it
#include<iostream.h>
#include<stdlib.h>
#include<process.h>
stu *next;
}*top,*newptr,*save,*ptr;
stu *Create(int,int);
void Push(stu*);
void Display(stu*);
void Pop();
int main()
top=NULL;
while(ch=='y'||ch=='Y')
newptr=Create(r,a);
93
if(newptr==NULL)
system("pause");
exit(1);
Push(newptr);
cin>>ch;
system("cls");
do
Display(top); system("pause");
cin>>ch;
if(ch=='y'||ch=='Y')
Pop();
}while(ch=='y'||ch=='Y');
return 0;
{ptr=new stu;
ptr->roll=n; ptr->age=m;
94
ptr->next=NULL;
return ptr;
{ if(top==NULL)
top=np;
else
{ save=top;
top=np;
np->next=save;
void Pop()
{ if(top==NULL)
cout<<"UNDERFLOW!!\n";
else
{ ptr=top; top=top->next;
delete ptr;
while(np!=NULL)
95
{ cout<<"\n(Rollno,age)->”<<np->roll<<",”<<np->age<<"->”;
np=np->next;
cout<<"!!!\n”;
OUTPUT:
(Rollno,age)->23,16->10,15->12,16->!!!
(Rollno,age)->10,15->12,16->!!!
SQL QUERIES
96
Table: EMPLOYEEINFO
ENO ENAME JOB MGR HIREDATE SAL COMM DPTNO
7369 MS Dhoni Clerk 7902 17-DEC-80 2800 20
7499 Ricky Ponting Salesman 7698 20-FEB-81 3600 300 30
7521 Rahul Dravid Salesman 7698 22-FEB-81 5250 500 30
7566 Sachin Tendulkar Manager 7839 02-APR-81 4975 20
7654 Steve Waugh Salesman 7698 28-SEP-81 6250 1400 30
7698 Sourav Ganguly Manager 7839 01-MAY-81 5850 30
7782 VVS Laxman Manager 7839 09-JUN-81 2450 10
7788 Shikhar Dhawan Analyst 7566 19-APR-87 5000 20
7839 Donald Bradman President 17-NOV-81 5000 10
7844 Piyush Chawla Salesman 7698 08-SEP-81 4500 30
7876 Arjuna Ranatunga Clerk 7788 23-MAY-87 6100 20
7900 Dale Steyn Clerk 7698 03-DEC-81 4950 30
7902 Rishi Kaushik Analyst 7566 03-DEC-81 3500 3600 20
7934 Cristiano Ronaldo Clerk 7782 23-JAN-82 5300 10
QUESTIONS:
A. To select all the columns of the above table
B. To list the name and employee number from the above table
C. To list the names, hiredate and salary of all the employees
D. To display the employee name and the incremented value of SAL as
SAL+300
E. To list ht employee name and annual salary(Annual salary=12*SAL+100)
F. Display the name and SAL where COMM is NULL
G. To list the DISTINCT department number from the table
H. To list the UNIQUE jobs from the table
I. To list the salary where salary is less than the commission
J. To list the salary between 3000 and 4000
K. To list the MGR which are between IN7902, 7566, 7788
L. To list the enames starting with ‘S’
M. To list all the columns where salary is greater than 3100
N. To list all the columns in ascending order of hiredate
O. To list all the columns in ascending order of DeptNo and descending order
of salary
P. To display the enames and job of employees hired between Feb 20,1981
and May 1,1981
97
Q. Display the enames and deptno of all the employees in department 20 and
30 in alphabetical ORDERBY name
R. To list the names and salary of all the employees who earn more than 1200
and are in department 10 or 40
S. To list the names and hiredate of all the employees who were hired in 1981
T. To list the employees who do not have managers
U. To list name and salary of all the employees where COMM is NOT NULL
V. To list names of all employees where the second letter of their name is ‘a’
W.To list the names and jobs of all the mployees who work in department 20
and their manager is 7788
X. To list the deptno,job and sum of salary group by deptno and job
Y. To list the enames which are not starting with ‘R’
Z. To add a new row containing the following data(7936, ‘Glen Mcgrath’,
‘Analyst’, 7799, ’23-Dec-82’, 5450, 20)
98
A. SELECT * FROM EMPLOYEEINFO;
C. SELECT ENAME,HIREDATE,SAL
FROM EMPLOYEEINFO;
ENO ENAME SAL
7369 MS Dhoni 2800
7499 Ricky Ponting 3600
7521 Rahul Dravid 5250
99
7566 Sachin Tendulkar 4975
7654 Steve Waugh 6250
7698 Sourav Ganguly 5850
7782 VVS Laxman 2450
7788 Shikhar Dhawan 5000
7839 Donald Bradman 5000
7844 Piyush Chawla 4500
7876 Arjuna Ranatunga 6100
7900 Dale Steyn 4950
7902 Rishi Kaushik 3500
7934 Cristiano Ronaldo 5300
100
Arjuna Ranatunga gets annual salary 73300
Dale Steyn gets annual salary 59500
Rishi Kaushik gets annual salary 42100
Cristiano Ronaldo gets annual salary 63700
F. SELECT ENAME,SAL
FROM EMPLOYEEINFO
WHERE COMM IS NULL;
ENAME SAL
MS Dhoni 2800
Sachin Tendulkar 4975
Steve Waugh 6250
VVS Laxman 2450
Shikhar Dhawan 5000
Donald Bradman 5000
Piyush Chawla 4500
Arjuna Ranatunga 6100
Dale Steyn 4950
Cristiano Ronaldo 5300
I. SELECT SAL
101
FROM EMPLOYEEINFO
WHERE SAL<COMM;
SAL
3500
J. SELECT SAL
FROM EMPLOYEEINFO
WHERE SAL BETWEEN 3000 AND 4000;
SAL
2800
3600
2450
4950
3500
K. SELECT MGR
FROM EMPLOYEEINFO
WHERE MGR IN(‘7902’,’7566’,’7788’);
MGR
7902
7566
7788
7566
L. SELECT ENAME
FROM EMPLOYEEINFO
WHERE ENAME LIKE “S%”;
ENAME
Sachin Tendulkar
Steve Waugh
Sourav Ganguly
Shikhar Dhawan
102
WHERE SAL>3100;
ENO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7499 Ricky Ponting Salesman 7698 20-FEB-81 3600 300 30
7521 Rahul Dravid Salesman 7698 22-FEB-81 5250 500 30
7566 Sachin Tendulkar Manager 7839 02-APR-81 4975 20
7654 Steve Waugh Salesman 7698 28-SEP-81 6250 1400 30
7698 Sourav Ganguly Manager 7839 01-MAY-81 5850 30
7788 Shikhar Dhawan Analyst 7566 19-APR-87 5000 20
7839 Donald Bradman President 17-NOV-81 5000 10
7844 Piyush Chawla Salesman 7698 08-SEP-81 4500 30
7876 Arjuna Ranatunga Clerk 7788 23-MAY-87 6100 20
7900 Dale Steyn Clerk 7698 03-DEC-81 4950 30
7902 Rishi Kaushik Analyst 7566 03-DEC-81 3500 3600 20
7934 Cristiano Ronaldo Clerk 7782 23-JAN-82 5300 10
103
7902 Rishi Kaushik Analyst 7566 03-DEC-81 3500 3600 20
7369 MS Dhoni Clerk 7902 17-DEC-80 2800 20
7654 Steve Waugh Salesman 7698 28-SEP-81 6250 1400 30
7698 Sourav Ganguly Manager 7839 01-MAY-81 5850 30
7521 Rahul Dravid Salesman 7698 22-FEB-81 5250 500 30
7900 Dale Steyn Clerk 7698 03-DEC-81 4950 30
7844 Piyush Chawla Salesman 7698 08-SEP-81 4500 30
7499 Ricky Ponting Salesman 7698 20-FEB-81 3600 300 30
P. SELECT ENAME,JOB
FROM EMPLOYEEINFO
WHERE HIREDATE BETWEEN ’20-FEB-81’ AND ’01-MAY-81’;
ENO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7499 Ricky Ponting Salesman 7698 20-FEB-81 3600 300 30
7521 Rahul Dravid Salesman 7698 22-FEB-81 5250 500 30
7566 Sachin Tendulkar Manager 7839 02-APR-81 4975 20
7698 Sourav Ganguly Manager 7839 01-MAY-81 5850 30
104
Rahul Dravid 5250
Steve Waugh 6250
Sourav Ganguly 5850
Piyush Chawla 4500
Dale Steyn 4950
T. SELECT ENAME
FROM EMPLOYEEINFO
WHERE MGR IS NULL;
7839 Donald Bradman
105
V. SELECT ENO, ENAME
FROM EMPLOYEEINFO
WHERE ENAME LIKE “_a%”;
ENO ENAME
7499 Ricky Ponting
7521 Rahul Dravid
7566 Sachin Tendulkar
7900 Dale Steyn
Y. SELECT ENAME
FROM EMPLOYEEINFO
WHERE ENAME NOT LIKE “R%”;
ENAME
VVS Laxman
Donald Bradman
Cristiano Ronaldo
MS Dhoni
Sachin Tendulkar
106
Shikhar Dhawan
Arjuna Ranatunga
Steve Waugh
Sourav Ganguly
Piyush Chawla
Dale Steyn
107
108