You are on page 1of 65

# Computer

Science

Practic
al
File

NameClass-XII-C

Simpl
e

Progr
ams

## Q1. Write a function that takes time as three integer

arguments(hours,minutes,seconds),and returns the number of
seconds since the clock last struck "12". Use thisfunction to
write a program to calculate the amount of time in seconds
betweentwo times, both of which are ithin one 12 hour cycle of
clock.
#include<iostream.h>
#include<conio.h>
#include<math.h>
int seconds(int hh,int mm,int ss)
{
int z,s;
z=abs(hh-12);
s=(z*60*60)+(mm*60)+ss;
return s;
}
int main()
{ clrscr();
int h1,h2,m1,m2,s1,s2,t1,t2;
cout<<"Enter the first time:"<<endl;
cout<<"\tHours:";
cin>>h1;
cout<<"\tMinutes:";
cin>>m1;
cout<<"\tSeconds:";
cin>>s1;
t1=seconds(h1,m1,s1);
cout<<"Enter the second time:"<<endl;
cout<<"\tHours:";
cin>>h2;
cout<<"\tMinutes:";
cin>>m2;
cout<<"\tSeconds:";
cin>>s2;
t2=seconds(h2,m2,s2);
cout<<"The difference in time is:"<<abs(t1-t2);
return 0;
}

OUTPUT:

## Q2. Write a program to find the sum of the sequence

#include<iostream.h>
#include<conio.h>
#include<math.h>
void sum(int x,int n)
{ double sum=0,s=0;
int k=1;
long fac=1;
for(int i=1;i<=n-1;i++)
{ for(int j=(2*k);j>=1;j--)
{fac*=j;}
sum+=(pow(x,i)/fac);
}
s=1+sum;
cout<<"\nThe sum is:\n";
cout<<s;
}
void main()
{ clrscr();
int x,n;
cout<<"\nEnter the limiting value\n";
cin>>n;
cout<<"\nEnter the value \n";
cin>>x;
sum(x,n);
getch();
}

OUTPUT:

## Q3. Write a c++ program to sum the sequence: -x -(x2/2!) +

(x4/4!) -(x6/6!) + .......
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
int x,p,i,j;
double fact=1.0,ans=0;
cout<<"Enter the value of x:";
cin>>x;
cout<<"Enter till what power you want:";
cin>>p;
ans=x;
for(i=2,j=1;i<=p;i++,j++){
fact=fact*i;
if(i%2==0)
ans+=(pow(-1,j))*((pow(x,i))/(fact));
}
cout<<"The sum of the series is:"<<ans;
return 0;
}

OUTPUT:

## Q4. Write a program to accept three digits and print all

possible combinations from these digits.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[3];
clrscr();
cout<<"Enter three digits :"<<endl;
cin>>a[0]>>a[1]>>a[2];
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for (int k=0;k<3;k++)
{
if (i!=j&&i!=k&&j!=k)
cout<<endl<<endl<<a[i]<<a[j]<<a[k];
}
}
}
getch();
}

OUTPUT:

## Q5. Computers are playing an increasing role in education. Write

a program that will help elementary school students learn
multiplication. Use rand function to produce two positive one
digit integers.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
void main()
{ clrscr();
int x[2],ans;
time_t t;
char c;
do
{ srand(time(&t));
x[0]=rand()%10;
srand(x[0]);
x[1]=rand()%10;
cout<<"\n\nWhat is "<<x[0]<<"times "<<x[1]<<" ?\nANS: ";
do
{
cin>>ans;
if(ans!=(x[0]*x[1]))
cout<<"\nWRONG! TRY AGAIN\n";
}while(ans!=(x[0]*x[1]));
if(ans==(x[0]*x[1]));
{
cout<<"correct!\n\n\nDO YOU WANT TO CONTINUE?";
cin>>c;
}
if(c=='N'||c=='n')
{

break;}
}while(1);
}
OUTPUT:

Q6. Write a program to find the LCM and GCD of two numbers.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int x,y,gcd=1;
cout<< "ENTER 1st NO : ";
cin>>x;
cout<<"\n\nEnter 2nd NO. :";
cin>>y;
for(int i=1;i<1000;++i)
{
if((x%i==0)&&(y%i==0))
gcd=i;
}
cout<<"\n\n\nGCD :"<<gcd;
cout<<"\n\n\nLCM :"<<(x*y)/gcd;
getch();
}

OUTPUT:

## Q7. Write a program to find the sum of sequence: 1/2!+ .........

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,i;
float sum=1.00;
float fact(int a);
cout<<"Enter n:";
cin>>n;
for(i=1;i<n;i++)
{sum+=(1/fact(i));}
cout<<"Sum of series ="<<sum;
getch();
}
float fact (int a)
{ int f=1,i;
for (i=1;i<=a;i++)
f*=i;
return f;
}

OUTPUT:

1 + 1/1! +

Q8.
Write a program in c++ to check if the entered number is
prime or not.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int p=0,i,n;
cout<<"Enter the no :"<<endl;
cin>>n;
for (i=2;i<=(n/2);i++)
{
if(n%i==0)
p=1;
}
if(p==0)
cout<<"Prime";
else if (p==1)
cout<<"Not Prime";
getch();
}

OUTPUT:

Q9.
Write a program to find the sum of series x2/1 + x3/1 +
x3/2 + x4/2 + x4/4 + x4/12 + .
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int j,n,x,f,i;
float s=1;
cout<<"Enter numerator & limit"<<endl;
cin>>x>>n;
for(i=1;i<n;i++)
{
for(j=1;j<i;j++)
{
f=f*j;
}
s=s+(pow(x,i)/f);
}
getch();
}

OUTPUT:

Q10.

## Write a program to find the sum of the series

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float b=0,x,c,n,s=0,e,r,f=1;
int i;
cout<<"Enter x & limit"<<endl;
cin>>x>>n;
for(i=2;i<=n;i++)
{
f=f*i;
if(i%2==0)
s=s+(pow(x,i)/f);
else
s=(s-(pow(x,i)/f));
}
cout<<"Result ="<<s;
getch();
}

OUTPUT:

Classan
d
Structu
re
Based

Progra
ms
Q1.
Write a program in c++ to store and display the data of
audio tape and book.
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
class publication
{
char title[20];
float price;
public:
void getdata()
{
cout<<"Enter Title"<<endl;
gets(title);
cout<<"Enter Price"<<endl;
cin>>price;

}
void putdata()
{
cout<<"Title"<<endl;
puts(title);
cout<<"Price"<<endl;
cout<<price;
}};
class book : protected publication
{
int pagecount;
public:
void putdata()
{
publication :: putdata();
cout<<endl<<"Pg Count"<<endl;
cout<<pagecount;
}
void getdata()
{
publication::getdata();
cout<<"Pg Count"<<endl;
cin>>pagecount;
}
};
class tape : protected publication
{
float time;
public:
void getdata()
{
publication :: getdata();
cout<<"Enter Time"<<endl;
cin>>time;
}
void putdata()
{
publication :: putdata();
cout<<endl<<"Time "<<endl;
cout<<time;
}
};
void main()
{
clrscr();
book b;
tape t;

int ch;
x:
{
cout<<endl<<"1. BOOK
cin>>ch;
switch(ch)
{
case 1:b.getdata();
b.putdata();
goto x;
case 2:t.getdata();
t.putdata();
goto x;
case 3:exit(0);
}
}
getch();
}

OUTPUT:

2:Tape

3:Exit "<<endl;

Q2.
Write a c++ program to illustrate a calculator. Perform
calculations on two operands using a class calculator.
Calculator should add, subtract, multiply and divide operands.
#include<iostream.h>
#include<conio.h>
#include<process.h>
class calculator
{
float result;
int o1,o2;
public:
void enter();
void showresult();
void sub();
void mul();
void div();
void clear();
};
void calculator::enter()
{
cout<<"Enter a operant:";
cin>>o1;
cout<<"Enter the other operant:";
cin>>o2;
}
void calculator::showresult()
{

## cout<<"The result of the operation is:"<<result<<endl;

}
{
result=o2+o1;
}
void calculator::sub()
{
result=o1-o2;
}
void calculator::mul()
{
result=o1*o2;
}
void calculator::div()
{
result=o1/o2;
}
void calculator::clear()
{
result=0;
}
int main()
{
char ch='y';
calculator c1;
while(ch=='y')
{
c1.enter();
cout<<"Which operation do you want to perform:";
cin>>ch;
switch(ch)
break;
case '-':c1.sub();
break;
case '*':c1.mul();
break;
case '/':c1.div();
break;
default :cout<<"Wrong choice:";
exit(0);
}
c1.showresult();
c1.clear();
cout<<"Do you want to continue:";
cin>>ch;

}
return 0;
}
OUTPUT:

Q3.
Imagine a ticket selling booth at a fair. People passing
by are requested to purchase a ticket. A ticket is priced at
Rs. 2.50. The booth keeps track of the number of people that
have visited the fair and of the total amount of money
collected. Model this ticket selling booth with a class tick.
Include a program to rest this class.
#include<iostream.h>
#include<conio.h>
#include<process.h>
class tick
{
int nop;
float total;
public:
tick()
{
nop=0;

total=0;
}
void inc();
void display();
void displaynop();
};
void tick::inc()
{
nop++;
total+=2.50;
cout<<"\nThe no. of people and the total have beem
incremented";
}
void tick::display()
{
cout<<"\nThe number of people who have entered the fair
are:"<<nop<<endl;
cout<<"The total amount collected till now is:"<<total<<endl;
}
void tick::displaynop()
{
cout<<"The no. of people who have visited so far
are:"<<nop<<endl;
}
int main()
{
char ch='y';
int choice;
tick t1;
l1:cout<<"\n\n\n\n1.Increment person and total"<<endl;
cout<<"2.Display no. of people and the amount collected till
now"<<endl;
cout<<"3.Display no. of people who entered"<<endl;
cout<<"4.Exit"<<endl;
cin>>choice;
switch(choice)
{
case 1:t1.inc();
goto l1;
break;
case 2:t1.display();
goto l1;
break;
case 3:t1.displaynop();
goto l1;
break;

case 4:exit(0);
break;
}
return 0;
}
OUTPUT:

Q4.
Write a program to accept information about a person and
then display it.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class person
{
char name[30];
long phone;
public:
person()
{
cout<<"\nConstructor initialised\n";
}
void getname()
{
cout<<"\nEnter the name\n";
gets(name);
cout<<"\nEnter the phone number\n";
cin>>phone;
}
void display()

cout<<"\nName\t";puts(name);
cout<<"\nPhone number\t"<<phone;

}
~person()
{
cout<<"\n******************\n";
}
};
class spouse:public person
{
char spousename[40];
public:
void getsp();
void display();
};
void spouse::getsp()
{
person::getname();
cout<<"\nEnter the spouse name\n";
gets(spousename);
}
void spouse::display()
{
person::display();
cout<<"Spouse name\t";puts(spousename);
}
void main()
{
clrscr();
spouse obj;
obj.getsp();
obj.display();
getch();
}

OUTPUT:

Q5.
Write a program to record a score of a cricket match.One
array stores information of batting team such as atman's name
runs scored, etc. The other array stores information about
bowling team. The program reads in above information and
depending on user's choice, it displays either batting team's
information or bowling team's information.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct bat
{
char name[20],modeout[70], indica;
int runs, score, totruns, totove, xtras;
};
struct bowl
{
char name[20];
int ttvrs,rnsgvn,wktstkn;
};
void main()
{
clrscr();
int plno;
int plytyp;
bat pl1[3];
bowl pl2[3];
cout<<"Enter the batsmen details:"<<endl;

## for (int i=0;i<3;i++)

{
cout<<"Enter name of player "<<i+1<<endl;
gets (pl1[i].name);
cout<<"enter the runs scored by player "<<i+1<<endl;
cin>>pl1[i].runs;
cout<<"Enter the overs played by the player"<<i+1<<endl;
cin>>pl1[i].totove;
cout<<"Enter the status of the player if out (N)or
not(Y)"<<endl;
cin>>pl1[i].indica;
}
cout<<"Enter the bowlers details "<<endl;
for (i=0;i<3;i++)
{
cout<<"Enter the name of the bowler "<<i+1<<endl;
gets(pl2[i].name);
cout<<"Enter the runs given by the bowler "<<i+1<<endl;
cin>>pl2[i].rnsgvn;
cout<<"Enter the wickets taken by the bowler "<<i+1<<endl;
cin>>pl2[i].wktstkn;
cout<<"Enter the total overs played by the bowler
"<<i+1<<endl;
cin>>pl2[i].ttvrs;
}
cout<<"Thank you all details recd"<<endl;
xyz:
cout<<"Select between batsmen(1) or bowlers(2) to see their
details"<<endl;
abc:
cin>>plytyp;
switch (plytyp)
{
case 1:
cout<<"Enter the batsman number to see his details
"<<endl<<endl<<endl;
cin>>plno;
plno--;
cout<<"Batsman number :"<<plno+1<<endl;
cout<<"Batsman name :";
puts(pl1[plno].name);
cout<<"Runs scored by the batsman :"<<pl1[plno].runs<<endl;
cout<<"Total overs played by the
batsman :"<<pl1[plno].totove<<endl;
cout<<"Player status out "<<pl1[plno].indica<<endl;
break;
case 2:

## cout<<"Enter the bowlers number to see his details

"<<endl<<endl<<endl;
cin>>plno;
plno--;
cout<<"Bowlers name :";
puts(pl2[plno].name);
cout<<"Runs given by the player is :"<<pl2[plno].rnsgvn<<endl;
cout<<"Total overs played by the
player :"<<pl2[plno].ttvrs<<endl;
cout<<"Total wickets taken by the
user :"<<pl2[plno].wktstkn<<endl;
break;
default:
cout<<"Idiot enter a decent value"<<endl;
goto abc;
}
cout<<endl<<endl<<endl<<"Do you wish to continue? Y-1 N2"<<endl;
cin>>plno;
if (plno==1)
goto xyz;
else
cout<<"Thank you Press any key to exit";
getch();
}
OUTPUT:

Q6.
Write a program to calculate the no. of types of guests
i.e.gentlemen or ladies or children using a class.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class counter
{
int count;
public:
counter()
{
count=0;
}
void incount()
{
count++;
}
int givecount()
{
return (count);
}
}c1,c2,c3;
void main()
{
clrscr();
char guest[10];
int i,a,b,c;
for(i=0;i<10;i++)
{
cin>>guest[i];
if(guest[i]=='g'||guest[i]=='G')
{
c1.incount();
a=c1.givecount();
}
else if(guest[i]=='l'||guest[i]=='L')
{
c2.incount();
b=c2.givecount();
}
else
{
c3.incount();
c=c3.givecount();
}

}
cout<<"GENTLEMEN :"<<a<<endl;
cout<<"CHILDREN :"<<c<<endl;
getch();
}
OUTPUT:

Q7.
Write a program to compute tax of a person using a class
taxpayer. Class taxpayer should the person's details of
account including pan no.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class taxpayer{
int pan;
char name[20];
long float tableinc;
double tax;
public:
void inputdata();
void display();
double computetax();
};
void taxpayer::inputdata()
{
cout<<"enterpersonal acct num:";
cin>>pan;
cout<<"\nenter the name of the person:";

gets(name);
cout<<"\nenter total annual taxable income:";
cin>>tableinc;
}
double taxpayer::computetax()
{
if(tableinc<=60000)
tax=0;
else if((tableinc>60000)||(tableinc<=150000))
tax= tableinc *0.05;
else if(tableinc>150000||tableinc<=500000)
tax=tableinc*0.1;
else
tax=tableinc*0.15;
return (tax);
}
void taxpayer::display()
{
cout<<"\npan num:"<<pan<<"\tname:";
puts(name);cout<<"\ttotal annual income:"<<tableinc<<"\tTax
payable:"<<tax;
}
void main()
{
clrscr();
taxpayer a;
a.inputdata();
clrscr();
a.computetax();
a.display();
getch();
}
OUTPUT:

Q8.
Write a c++ program to calculate
created of a particular class type.

the

#include<iostream.h>
#include<conio.h>
class just
{
int x;
public:
void enter()
{ cout<<"Enter a value of x:";
cin>>x;
}
void out()
{
cout<<"The value is:"<<x<<endl;
}
};
int main()
{
just j;
char ch='y';
int i=0;
while(ch=='y')
{
j.enter();
i++;
cout<<"Do you eant to continue:";
cin>>ch;
}
cout<<"The number of objects created are:"<<i;
return 0;
}
OUTPUT:

no.

of

objects

## Q9. Write a c++ program using a class student to calculate the

percentage of marks obtained by him.
#include<iostream.h>
#include<conio.h>
class student
{
int roll_no;
char name[20];
char class_st[8];
int marks[5];
float percentage;
float calculate();
public:
void displaymarks();
};
float student::calculate()
{
percentage=0;
for(int i=0;i<5;i++)
percentage+=marks[i];
percentage=(percentage/5);
return percentage;
}
{
cout<<"Enter the roll no.:";
cin>>roll_no;
cout<<"Enter the name:";
cin>>name;
cout<<"Enter the class studing in:";
cin>>class_st;
cout<<"Enter the marks:"<<endl;
for(int j=0;j<5;j++){
cout<<"\tEnter mark "<<j+1<<":";
cin>>marks[j];
}
}
void student::displaymarks()
{
cout<<"Roll no:"<<roll_no<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Class:"<<class_st<<endl;
cout<<"Percentage:"<<calculate()<<endl;
}
int main()

{
student s1;
s1.displaymarks();
return 0;
}
OUTPUT:

## Q10. Write a c++ program using class serial to store and

display serial's title, duration, number of episodes, serial
code.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class serial
{
int code;
char title[20];
float duration;
int noe;
public:
serial()
{
duration=30;
noe=103;
}
void newserial();
void otherentries(int dur,int no);
void displaydata();
};

void serial::newserial()
{
cout<<"\n\nEnter the serial code:";
cin>>code;
cout<<"Enter the title:";
gets(title);
}
void serial::otherentries(int dur,int no)
{
duration=dur;
noe=no;
}
void serial::displaydata()
{
cout<<"\tSerial code is:"<<code<<endl;
cout<<"\tTitle is:"<<title<<endl;
cout<<"\tDurations is:"<<duration<<endl;
cout<<"\tNo. of episodes are:"<<noe<<endl<<endl;
}
int main()
{
clrscr();
char ch='y';
int i=0,dur,no;
serial s1[10];
while(ch=='y')
{
s1[i].newserial();
cout<<"Enter the duration and the no. of episodes:";
cin>>dur>>no;
s1[i].otherentries(dur,no);
i++;
cout<<"\n\nDo you want to continue:";
cin>>ch;
}
cout<<"\n\nThe details you have entered are:"<<endl<<endl;
for(int j=0;j<i;j++){
cout<<"Data of serial "<<j+1<<" is:"<<endl;
s1[j].displaydata();
}
return 0;
}

OUTPUT:

Data
Structu
res

Based
Progra
ms
Q1.
Write a program in c++ to find the sum of diagonals, rows
and columns of a given matrix.
#include<iostream.h>
#include<conio.h>
#include<process.h>
int sum1(int a[50][50],int r)
{

int s=0;
for(int i=0;i<r;i++)
{ s=s+a[i][i];}
return(s);
}
int sum2(int a[50][50],int r)
{
int s=0;
int j;
j=r-1;
for(int i=0;i<r;i++)
s=s+a[i][j--];
return(s);
}
void row(int a[50][50],int r)
{
int s=0;
for(int i=0;i<r;i++)
{
s=0;
for(int j=0;j<r;j++)
{s=s+a[i][j];}
cout<<"Sum of Row "<<i+1<<" = "<<s<<endl;
}}
void col(int a[50][50],int r)
{ int s=0;
for(int i=0;i<r;i++)
{
s=0;
for(int j=0;j<r;j++)
{
s=s+a[j][i];
}
cout<<"Sum of Column "<<i+1<<" = "<<s<<endl;
}}
void main()
{
clrscr();
int i,s,j,r,c,ch,a[50][50];
x:
cout<<"Entr Array Limit--(Enter only Row as R=C)"<<endl;
cin>>r;
cout<<"Enter Array"<<endl;
for(i=0;i<r;i++)
{

for(j=0;j<r;j++)
{
cin>>a[i][j];
}
}

y:
cout<<endl<<endl<<"
Enter
Choice :"<<endl<<"Sum of---- 1:main diagonal 2:Secondary
diagonal 3.Rows 4.Columns 5.Re-enter 6.Exit "<<endl;
cin>>ch;
switch(ch)
{
case 1:
s=sum1(a,r);
cout<<"Sum = "<<s<<endl;
goto y;
case 2 :
s=sum2(a,r);
cout<<"Sum = "<<s<<endl;
goto y;
case 3:
row(a,r);
goto y;
case 4:
col(a,r);
goto y;
case 5:
goto x;
case 6:
exit(0);
default :
cout<<"Wrong Choice"<<endl;
break;
}
getch();
}
OUTPUT:

Q2.
Write a program in c++ to implement circular queue.
#include<iostream.h>
#include<conio.h>
#include<process.h>
class queue
{int data[10];
int front,rear;
public:
queue()
{front=-1;
rear=-1;
}
void remove();
void display();
};
{if((rear+1==front)||(rear==9&&front==0))
{cout<<"Overflow ";
}
else
{if((rear==-1) &&(front==-1))
{rear=0;
front=0;
}
else if(rear==9)
{rear=0;

else
{rear++;
}
cout<<"Enter the element ";
cin>>data[rear];
}

void queue::remove()
{if(front==-1&&rear==-1)
{cout<<"Underflow ";
}
else
{if(front==9)
{front=0;
}
else if(front==rear)
{front=-1;
rear=-1;
}
else
{front++;
}
}
}
void queue::display()
{int i=0,n=10;
if(rear==-1)
{cout<<"No elements.."<<endl;
}
else
{ if(rear>=front)
{for(i=0;i<front;i++)
{cout<<"_ ";
}
for(i=front;i<=rear;i++)
{cout<<data[i]<<" ";
}
for(i=rear+1;i<n;i++)
{cout<<"_ ";
}
}
else
{for(i=0;i<=rear;i++)
{cout<<"_ ";

for(i=rear+1;i<front;i++)
{cout<<data[i]<<" ";
}
for(i=front;i<n;i++)
{cout<<"_ ";
}
} }
}
void main()
{clrscr();
int ch;
queue queue;
X:
cout<<"\nEnter your
choice\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";
cin>>ch;
switch(ch)
goto X;
case 2:queue.remove();
goto X;
case 3:queue.display();
goto X;
case 4:exit(0);
}
getch();
}
OUTPUT:

Q3.
Write a program in c++ to sort the given array using
bubble sort.
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int ch;
int i,j,x,k,z,l,m,n,o,p,a[50],small;
q :
cout<<"Enter the choice 1:Selection 2:Bubble 3:Exchange
Selection 4:Insertion 5:Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the limit "<<endl;
cin>>n;
cout<<endl;
cout<<"Enter the elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
cout<<endl;
}
for(j=0;j<n;j++)
{
small=a[j];
p=j;
for(i=j;i<n;i++)
{
if(small>a[i])
{
small=a[i];
p=i;
}
}
for(k=p;k>j;k--)
{
a[k]=a[k-1];
}
a[j]=small;
}
cout<<"Result"<<endl;

for(z=0;z<n;z++)
{
cout<<a[z];
cout<<endl;
}goto q;
case 2:
cout<<"Enter the limit"<<endl;
cin>>n;
cout<<"Enter the elements"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
for(j=0;j<n;j++)
{
for(i=0;i<n-1;i++)
{
if (a[i]>a[i+1])
{
x=a[i+1];
a[i+1]=a[i];
a[i]=x;
}
}
}
cout<<"Result"<<endl;
for (i=0;i<n;i++)
{
cout<<a[i];
cout<<endl;
}
break;
case 3 :
cout<<"Enter the limit "<<endl;
cin>>n;
cout<<endl;
cout<<"Enter the elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
cout<<endl;
}
for(j=0;j<n;j++)
{
small=a[j];
p=j;
for(i=j;i<n;i++)

{
if(small>a[i])
{
small=a[i];
p=i;
}
}
a[p]=a[j];
a[j]=small;
}
cout<<"Result"<<endl;
for(z=0;z<n;z++)
{
cout<<a[z];
cout<<endl;
}
goto q;
case 4 :
int m=-32767;
cout<<"enter the no. of elements"<<endl;
cin>>n;
cout<<"enter the array"<<endl;
for(i=1;i<=n;i++)
{cin>>a[i];}
a[0]=m;
for(i=1;i<=n;i++)
{for(j=0;j<i;j++)
{if(a[i]<a[j])
{p=a[i];
for(k=i-1;k>=j;k--)
{a[k+1]=a[k];}
a[j]=p;}}}
for(i=1;i<=n;i++)
{cout<<a[i];}
goto q;
case 5:
exit(0);
default:
cout<<"Wrong choice";
goto q;
}
getch();
}

OUTPUT:

Q4.
Write a program in c++ to implement stack using linked
list.
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
struct node
{char name[20];
int age;
}*ptr=NULL,*save=NULL;
class stack
{node *top;
public:
stack()
{top=NULL;
}
void stackpush();
void stackpop();
void display();
}st;
void stack::stackpush()
{ptr=new node;
if(ptr==NULL)
{cout<<"Overflow ";
}

else
{cout<<"Enter the name ";
gets(ptr->name);
cout<<"Enter the age ";
cin>>ptr->age;
if(top==NULL)
{top=ptr;
}
else
top=ptr;
}
}
}
void stack::stackpop()
{if(top==NULL)
{cout<<"Underflow ";
}
else
{save=top;
cout<<"Name ";
puts(save->name);
cout<<"Age "<<save->age;
delete save;
}
}
void stack::display()
{if(top==NULL)
{cout<<"No elements.."<<endl;
}
else
{ptr=top;
while(ptr!=NULL)
{cout<<"\nName ";
puts(ptr->name);
cout<<"Age "<<ptr->age;
}
}
}
void main()
{clrscr();
int ch;

X:
cout<<"\nEnter your
choice\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";
cin>>ch;
switch(ch)
{case 1:st.stackpush();
goto X;
case 2:st.stackpop();
goto X;
case 3:st.display();
goto X;
default:cout<<"Wrong choice ";
goto X;
case 4:exit(0);
}
getch();
}
OUTPUT:

Struct
ured
Query

Langu
age
Querie
s

Q1. Consider the following tables Product and Client. Write SQL commands for the statement (i) to (iv) and give outputs for
SQL queries (v) to (viii).
Table: PRODUCT
P_ID

Product
Name

Manufacture
Price
r

TP01

TalcomPowde
LAK
r

40

ABC

45

ABC

55

SH06 Shampoo

XYZ

120

## FW12 Face Wash

XYZ

95

Table: CLIENT
C_ID Client Name

City

P_ID

01

TalcomPowder Delhi

FW05

06

Face Wash

Mumbai BS01

12

Bath Soap

Delhi

SH06

15

Shampoo

Delhi

FW12

16

Face Wash

Banglore TP01

## (i) To display the details of those Clients whose city is Delhi.

Ans: Select all from Client where City=Delhi;
(ii) To display the details of Products whose Price is in the range of 50 to 100(Both values included).
Ans: Select all from product where Price between 50 and 100;
(iii) To display the ClientName, City from table Client, and ProductName and Price from table Product, with their
corresponding matching P_ID.
Ans: Select ClientName,City,ProductName, Price from Product,Client where Product.P_ID=Client.P_ID;
Ans: Update Product Set Price=Price +10;
(v) SELECT DISTINCT Address FROM Client.
Ans:

City
Delhi
Mumbai
Bangalore

(vi) SELECT Manufacturer, MAX(Price), Min(Price), Count(*) FROM Product GROUP BY Manufacturer;
Ans:

Manufactur
er

Max(Pric
e)

Min(Pric
e)

LAK

40

ABC

55

45

XYZ

120

95

40

Count(*)

Ans:

ClientName

ManufacturerName

CosmeticShop

ABC

TotalHealth

ABC

LiveLife

XYZ

PrettyWoman

XYZ

Dreams

LAK

## (viii) SELECT ProductName, Price * 4 FROM Product.

Ans:

ProductNam
e

Price*
4

TalcomPowede
r

160

FaceWash

180

BathSoap

220

Shampoo

480

Face Wash

380

Q2. Consider the following tables Item and Customer. Write SQL commands for the statement (i) to (iv) and give outputs
for SQL queries (v) to (viii).
Table: ITEM
C_ID

ItemName

Manufacturer Price

PC01

Personal
Computer

ABC

35000

LC05

Laptop

ABC

55000

PC03

Personal
Computer

XYZ

32000

PC06

Personal
Computer

COMP

37000

PQR

57000

LC03 Laptop

Table: CUSTOMER
C_ID CustomerName City

P_ID

01

N.Roy

Delhi

LC03

06

H.Singh

Mumbai

PC03

12

R.Pandey

Delhi

PC06

15

C.Sharma

Delhi

LC03

16

K.Agarwalh

Banglore PC01

## (i) To display the details of those Customers whose city is Delhi.

Ans: Select all from Customer Where City=Delhi;
(ii) To display the details of Item whose Price is in the range of 35000 to 55000 (Both values included).
Ans: Select all from Item Where Price>=35000 and Price <=55000;
(iii) To display the CustomerName, City from table Customer, and ItemName and Price from table Item, with their
corresponding matching I_ID.
Ans: Select CustomerName,City,ItemName, Price from Item,Customer where Item.I_ID=Customer.I_ID;
(iv) To increase the Price of all Items by 1000 in the table Item.
Ans: Update Item set Price=Price+1000;
(v) SELECT DISTINCT City FROM Customer.
Ans:

City
Delhi
Mumbai
Bangalore

Ans:

ItemName

Max(Price
)

Count(*
)

PersonalCompute
r

37000

Laptop

57000

## (vii) SELECT CustomerName, Manufacturer FROM Item, Customer WHERE Item.Item_Id=Customer.Item_Id;

Ans:

CustomerName

Manufacturer Name

N.Roy

PQR

H.Singh

XYZ

R.Pandey

COMP

C.Sharma

PQR

K.Agarwal

ABC

(viii) SELECT ItemName, Price * 100 FROM Item WHERE Manufacturer = ABC;
Ans:

ItemName

Price*100

PersonalCompute
r

3500000

Laptop

5500000

Q3. Consider the following tables Consignor and Consignee. Write SQL command for the statements(i)to(iv) And give
outputs for the SQL quries (v) to ( viii).
TABLE : CONSIGNOR
CnorID

CnorName

City

ND01

R singhal

24,ABC Enclave

New Delhi

ND02

AmitKumar

## 123,Palm Avenue New Delhi

MU15

R Kohil

5/A,South,Street Mumbai

MU50

S Kaur

7-K,Westend

Mumbai

TABLE : CONSIGNEE
CneeID

CnorID CneeName

CneeCity

MU05

ND01

ND08

ND02

P Dhingr a

KO19

MU15

A P Roy

## 2A,Central/ avenue Kolkata

MU32

ND0 2

S mittal

P 245, AB Colony

Mumbai

ND48

MU5 0

B P jain

13,Block d,a,viha

New Delhi

Mumbai

## (i) To display the names of all consignors from Mumbai.

Ans: Select CnorName from Consignor where city=Mumbai;
(ii) To display the cneeID, cnorName, cnorAddress, CneeName, CneeAddress for every Consignee.
Consignor.CnorId=Consignee.CnorId;
(iii) To display the consignee details in ascending order of CneeName.
Ans: Select * from Consignee Orderby CneeName Asc;
(iv) To display number of consignors from each city.
Ans: Select city, count(*) from Consignors group by city;
(v) SELECT DISTINCT City FROM CONSIGNEE;
Ans:
CneeCity
Mumbai
New Delhi
Kolkata
(vi) SELECT A.CnorName, B.CneeName FROM Consignor A, Consignee B WHERE A.CnorID=B.CnorID AND
B.CneeCity=Mumbai;
Ans:

CnorName

CneeName

(vii) SELECT CneeName,CneeAddress FROM Consignee WHERE CneeCity Not IN (Mumbai, Kolkata);
Ans:

CneeName
P Dhingr a
B P jain

16/j,Moore Enclave
13,Block d,a,viha

(viii) SELECT CneeID, CneeName FROM Consignee WHERE CnorID = MU15 OR CnorID = ND01;
senderCity
New Delhi
Mumbai
Ans:

CneeID

CneeNam
e

MU05

Rahul

KO19 A P Roy

Kishore

Q4. Consider the following tables. Write SQL command for the statements (i)to(iv)and give outputs for the SQL quries
(v) to (viii).
TABLE : SENDER
SenderID SenderName

Sender

Sender
City

ND01

R jain

2,ABC Appts

New Delhi

MU02

H sinha

12, Newton

Mumbai

MU1 5

S haj

27/
Street

New Delhi

ND5 0

122-K,SDA

A,Park

Mumbai

TABLE :RECIPIENT
5,Central
Avenue

ReCCity

KO05 ND01

RBajpayee

Kolkata

ND08 MU0 2

MU19 ND01

H sing

MU32 MU1 5

ND48 ND50

S Tripathi

NewDelhi

13, B1 D,Mayur
NewDelhi
Vihar

## (i) To display the names of all senders from Mumbai.

Ans: Select * from Sender where SenderCity =Mumbai;
(ii) To display the recID, senderName, senderAddress, RecName, RecAddress for every recipt.
Sender.Senderid=Recipient.RenderId;

RecName,

## (iii) To display the sender details in ascending order of SenderName.

Ans: Select * from Sender order by SenderName;

from

Sender,

Recipient

where

## (iv) To display number of Recipients from each city.

Ans: Select RecCity,Count(*) from Recipient group by RecCity;
(v) SELECT DISTINCT SenderCity FROM Sender;
Ans:

Sender City
New Delhi
Mumbai

(vi) SELECT A.SenderName A, B.RecName FROM Sender A, Recipient B WHERE A.SenderID=B. SenderID AND
B.RecCity=Mumbai;
Ans:

SenderNam
e
R.Jain

S.Jha

RecNam
e
H.Singh

P.K.Swamy

Ans:

RecName

Mahajan

116,A Vihar

S Tripati

## 13, B1 D, Mayur Vihar

(viii) SELECT RecID, RecName FROM Recipient WHERE SenderID = MU02 OR SenderID = ND50;
Ans:

RecID

RecName

ND08

S Mahajan

ND48

S Tripathi

Q5. Study the following tables FLIGHTS and FARES and write SQL commands for the questions (i) to (iv).
TABLE: FLIGHTS
FL_NO

STARTING ENDING

NO_
FLGHTS

NO_
STOPS

IC301

MUMBAI

DELHI

IC799

BANGALORE DELHI

MC101

INDORE

MUMBAI

IC302

DELHI

MUMBAI

AM812

KANPUR

BANGLORE

IC899

MUMBAI

KOCHI

AM501

DELHI

TRIVENDRUM 1

MU499

MUMBAI

IC701

DELHI

TABLE:FLIGHTS
FL_NO AIRLINES
IC701

FARE TAX%

INDIAN
AIRLINES

6500

10

MU499 SAHARA

9400

AM501

JET AIRWAYS

13450

IC899

INDIAN
AIRLINES

8300

IC302

INDIAN
AIRLINES

4300

10

IC799

INDIAN
AIRLINES

1050

10

MC101

DECCAN
AIRLINES

3500

(i) Display FL_NO and NO_FLIGHTS from KANPUR TO BANGALORE from the table FLIGHTS.
Ans: Select FL_NO, NO_FLIGHTS from FLIGHTS where Starting=KANPUR AND ENDING=BANGALORE;
(ii) Arrange the contents of the table FLIGHTS in the ascending order of FL_NO.
Ans: SELECT * From FLIGHTS ORDER BY FL_NO ASC;
(iii) Display the FL_NO and fare to be paid for the flights from DELHI to MUMBAI using the tables FLIGHTS and
FARES, where the fare to paid = FARE+FARE+TAX%/100.
Ans: Select FL_NO,
Ending=MUMBAI;

FARE+FARE+(TAX%/100)

from

FLIGHTS,

FARES

where

Starting=DELHI

AND

(iv) Display the minimum fare Indian Airlines is offering from the tables FARES.
Ans: Select min(FARE) from FARES Where AIRLINES=Indian Airlines;
Q6. Study the following tables DOCTOR and SALARY and write SQL commands for the questions (i) to (iv) and give
outputs for SQL queries (v) to (vi) :
TABLE: DOCTOR
ID NAME DEPT

SEX EXPERIENCE

101 Johan

ENT

12

104 Smith

ORTHOPEDIC M

107 George

CARDIOLOGY M

114 Lara

SKIN

MEDICINE

109

K
George

10

10

117 Lucy

ENT

111 Bill

MEDICINE

12

15

TABLE: SALARY
ID

## BASI ALLOWANC CONSULTAIO

C
E
N

101 12000

1000

300

104 23000

2300

500

107 32000

4000

500

114 12000

5200

100

109 42000

1700

200

105 18900

1690

300

130 21700

2600

300

(i) Display NAME of all doctors who are in MEDICINE having more than 10 years experience from the Table
DOCTOR.
Ans: Select Name from Doctor where Dept=Medicine and Experience>10;
(ii) Display the average salary of all doctors working in ENTdepartment using the tables. DOCTORS and SALARY
Salary =BASIC+ALLOWANCE.
Ans: Select avg(basic+allowance) from Doctor,Salary where Dept=Ent and Doctor.Id=Salary.Id;
(iii) Display the minimum ALLOWANCE of female doctors.
Ans: Select min(Allowance) from Doctro,Salary where Sex=F and Doctor.Id=Salary.Id;
(iv) Display the highest consultation fee among all male doctors.
Ans: Select max(Consulation) from Doctor,Salary where Sex=M and Doctor.Id=Salary.Id;
(v) SELECT count (*) from DOCTOR where SEX = F
Ans: 4
(vi) SELECT NAME, DEPT , BASIC from DOCTOR, SALRY Where DEPT = ENT AND DOCTOR.ID = SALARY.ID
Ans:

Name

Dept

Basic

Jonah

Ent

12000

Q7. Consider the following tables EMPLOYEES and EMPSALARY. write SQL commands for the Statements (i) to (iv) and
give outputs for SQL quires (v) to (viii).

CITY

010

GEORGE

Smith

105

MARY

Jones

842VineAve

Losantiville

152

SAM

Tones

33 Elm st

Paris

215

SARAH

Ackerman

440 U.S.110

Upton

244

MANILA

Sengupta

24
FriendsStreet

New Delhi

300

ROBERT

Samuel

9 Fifth Cross

Washington

335

HENRY

Williams

## 12 Moore Street Boston

400

RACHEL

Lee

121 Harrison

New York

441

PETER

Thompson

Paris

EMPSALRAY
EMPID SALARY

BENEFIT DESIGNATIO
S
N

010

75000

15000

Manager

105

65000

15000

Manager

152

80000

25000

Director

215

75000

12500

Manager

244

50000

12000

Clerk

300

45000

10000

Clerk

335

40000

10000

Clerk

400

32000

7500

Salesman

441

28000

7500

Salesman

(i) To display Firstname, Lastname, Address and City of all employees living in Paris from the table EMPLOYEES.
Ans: Select Firstname,Lastname,Address,City from Employees where City=Paris;
(ii) To display the content of EMPLOYEES table in descending order of FIRSTNAME.
Ans: Select * from Employees Order By Firstname Desc;
(iii) To display the Firstname, Lastname, and Total Salary of all managers from the tables, where Total Salary is
calculated as Salary+Benifts.
Ans: Select Firstname,Lastname,Salary+Benefits from Employees, Empsalary where Designation=Manager and
Employees.EmpId=EmpSalary.EmpId;
(iv) To display the Maximum salary among Managers and Clerks from the table EMPSALARY.
Ans: Select Designation,max(Salary) from EmpSalary where Designation=Manager or Designation=Clerk;
(v) SELECT FIRSTNAME,SALARY FROM EMPLOYEES,EMPSALARY WHERE DESTINATION =SalesmanAND
EMPOLYEES.EMPID=EMPSALARY.EMPID ;

Ans:

Firstnam
e

Salar
y

Rachel

3200
0

Peter

2800
0

## (vi) SELECT COUNT (DISTINT DESIGNATION ) FROM EMPSALARY

Ans: 4
(vii) SELECT DESIGNATION , SUM(SALARY) FROM EMPSALARY GROUP BY DESIGNATION HAVING
COUNT(*)>2;
Ans:

Designation

Sum(Salary)

Manager

215000

Clerk

135000

## (viii) SELECT SUM (BENEFITS) FROM EMPSALARY WHERE DESIGNATION=Clerk;

Ans: 32000
Q8. Consider the following tables WORKERS and DESIG. Write SQL commands for the statements (i) to (iv) and give
outputs for SQL queries (v) to (viii).

WORKERS

DESIG

CITY

102

Sam

Tones

33 Elm St.

Paris

105

Sarah

Ackerman

44 U.S.110

NewYork

144

Manila

Sengup ta

24
Street

New Delhi

210

George

Smith

83 First Street

Howard

255

Mary

Jones

Losantiville

300

Robert

Samuel

9 Fifth Cross

Washington

335

Henry

Williams

403

Ronny

Lee

## 121 Harrison St. New York

451

Pat

Thomps on

Friends

Paris

W_ID

SALARY BENEFITS

DESIGINA
TION

102

75000

15000

Manager

105

85000

25000

Director

144

70000

15000

Manager

210

75000

12500

Manager

255

50000

12000

Clerk

300

45000

10000

Clerk

335

40000

10000

Clerk

400

32000

7500

Salesman

451

28000

7500

Salesman

(i) To display W_ID Firstname, address andCity of all employees living in New York fromthe Table WORKERs
Ans: select W_ID ,firstname,address,city from workers where city=New York;
(ii) To display the content of workers table in ascending order of LASTNAME.
Ans:Select * from Worker Order By lastname Asc;
(iii) To display the FIRSTNAME, LASTNAME and Total Salary of all Clerks from the tables WORKERS And DESIG,
where Total salary is calculated as Salary + benifts.
Ans: Select firstname, lastname, salary+benefits where worker.w_id=desg.w_id and Designation=Clerk;
(iv) To display the minimum salary among managers and Clerks from the tables DESIG.
Ans: Selet DESIGNATION,min(SALARY) From DESIG Group By DESIGNATION Having DESIGNATION
In(Clerk,Manager);
(v) SELECT FIRSTNAME, SALARY FROM WORKERS, DESIG WHERE DESIGINATION = MANAGER AND
WORKERS.W_ID = DESIGN.W_ID
Ans:

FIRSTNAM
E

SALAR
Y

Sam

75000

Manila

70000

George

75000

## (vi)SELECT COUNT(DISTINCT DESIGNATION) FROM DESIGN ;

Ans: 4
(vii) SELECT DESIGNATION, SUM(SALARY) FROM DESIG GROUP BY DESIGNATION HAVING COUNT (*) < 3;

Ans:

Designati
on
Director
Salesman

Sum(Salar
y)
85000
60000

Ans: 15000

## Q9. Give the following table for database a LIBRARY.TABLE : BOOKS

BOOK_I BOOK_NAM AUTHONAM PUBLISHE PRIC
QUANTIT
TYPE
D
E
E
R
E
Y
William
Hopkins

F0001

The Tears

F0002

T0001

My first C+ +

Brains
Brooke

T0002

C++
works

C001

Fast Cook

Brain

First Publ

750

Fiction

10

First Publ.

700

Fiction

EPB

250

Text

10

325

Text

Lata Kapoore

350

Cooker
y

&

EPB

TABLE:ISSUED

## Write SQL queries from b to g.

(i)To show Book name, Author name
Ans: select Book_name,Author_name,
=EPB;

BOOK_I QUANTITY_ISSU
D
ED
F0001

T0001

C0001

price

from

books

## (ii) To list the names of the books of FICTIONS type.

Ans: Select Book_name from books where type=FICTION;
(iii) To display the names and prices of the books in descending order of their price.
Ans: Select Book_name, price from books order by price desc;
(iv) To increase the price of all books of First Pub.by 50.
Ans: update books set price= price+50 where publishers = First Publ;
(v) To Display the Book_ID, Book_Name and Quantity Issued for all books Which have been issued.

where

Publisher

## Ans:Select Book_ID, Book_Name, Quantity_Issued from Books,Issued where Books.BookId=Issued.BookId;

(vi) To insert a new row in the table Issued having the following data: F0002,4
Ans: insert into Issued values(F0002,4);
(vii) Give the output of the following queries on the above tables:
(1) Select Count(Distinct Publishers) From Books;
Ans: 3
(2) Select Sum(Price) From Books Where Quantity>5;
Ans: 1350.
(3) Select Book_Name,Author_Name From Books Where Price<500;
Ans: Book_Name Author_Name My First C++ Brian & Brooks C++ Brainworks A.W.Rossaine Fast Cook Lata Kapoor.
(4) Select Count(*) From Books;
Ans: 5
Q10.
Write SQL commands for (b) to (g) and write the outputs for (h) on the basis of tables TNTERIORS and
NEWONES.
TABLE: INTERIORS
NO

ITEM
NAME

TYPE

Red rose

DoubleBed

23/02/02

32000

15

Soft touch

Baby cot

20/01/02

9000

10

19/02/02

8500

10

01/01/02

20000

20

Comfort
zone

Double Bed

12/01/02

15000

20

Jerry look

Baby cot

24/02/02

7000

19

Lion king

Office Table

20/02/02

16000

20

22/02/02

30000

25

13/12/01

9000

15

10

Dine

11000

15

## DATEOFSTOCK PRICE DISCOUNT

DinningTable 19/02/02

TABLE:NEWONES
NO

ITEMNAM
TYPE
E

## DATEOFSTOC PRIC DISCOUN

K
E
T

11 White wood

Doublebe
23/03/03
d

20000

20

12 James007

Sofa

20/02/03

15000

15

13 Tom look

Baby cot

21/02/03

7000

10

(i) To show all information about the sofas from the INTERIORS table.
Ans: Select * from INTERIORS where type= sofa;
(ii) To list ITEMNAME and TYPE of those items, in which DATEOFSTOCK is before 22/01/02 from the INTERIORS
table in descending order of ITEMNAME.
Ans: Select Itemname,Type From Interiors Where Dateofstock<{22/01/02} order by Itemname;
(iii) To display ITEMNAME and DATEOFSTOCK of those items in which the Discount percentage is more than 15
from INTERIORS.
Ans: Select Itemname,Dateofstock from Interiors Where Discount>15;
(iv) To count the number of items whose type is Double bed;
Ans: Select Count(*) from Interiors Where Type=Double Bed;
(v) To insert new row in the NEWONES table with the following data:14, True Indian , Office Table ,
{28/03/03},15000,20.
Ans: Insert into Newones values(14,True Indian,Office Table,{28/03/03},15000,20);
(vi) Give the outputs for the following SQL statements.
(1) Select COUNT (distinct TYPE) from INTERIORS;
Ans: 5
(2) Select AVG(DISCOUNT)from INTERIORS where TYPE =Baby cot;
Ans: 13
(3) Select SUM(price)from INTERIORS where DATEOFSTOCK<{12/02/02};
Ans: 53000