Sunteți pe pagina 1din 139

C++ Programming Class XI & XII 2013

CLASS XI
GETTING STARTED WITH C++

Q.1 Write a c++ program that accept radius of a circle and prints its
area?
DATE:5|12|2011
FILENAME:g1.cpp

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int R;
float A;
cout<<"Enter the radius of circle";
cin>>R;
A=(22/7)*R*R;
cout<<"The area of circle is"<<A;
}
INPUT:
Enter radius of circle 4
OUTPUT:
The area of circle is 48

Q.2 Write a c++ program that excepts marks in 5 subject and output
average marks?
DATE:5|12|11
FILENAME:g2.cpp

#include<iostream.h>

VAIBHAV PANDEY Page 1


C++ Programming Class XI & XII 2013

#include<conio.h>
void main()
{ clrscr();
float sub1,sub2,sub3,sub4,sub5,average,marks;
cout<<"Enter the marks obtained in 5 subjects";
cin>>sub1>>sub2>>sub3>>sub4>>sub5;
marks=sub1+sub2+sub3+sub4+sub5;
average=(marks/5);
cout<<"The average marks are"<<average;
}
INPUT:
Enter marks obtained in 5 subject
sub1 45
sub2 89
sub3 90
sub4 90
sub5 90
OUTPUT:
The average marks are 80.80

Q.3 Write a program to find area of rectangle?


DATE:5|12|11
FILENAME:g3.cpp

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int l,b;
float a;
cout<<"Enter length and breath of a rectangle";
cin>>l>>b;
a=(l*b);
cout<<"Area of rectangle is"<<a;
}

VAIBHAV PANDEY Page 2


C++ Programming Class XI & XII 2013

INPUT:
Enter length 6
Enter breath 4
OUTPUT:
Area of rectangle 24

Q.4 Write a c++ program to find out the largest number out of 'n'
number?
DATE:5|12|11
FILENAME:g4.cpp

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,x,c=0,ln=0;
cout<<"Enter value of n";
cin>>n;
while(c<=n)
{
cout<<"Enter value of x";
cin>>x;
if(x>=ln)
{ln=x;
}
c=c+1;
}
cout<<"The largest value is"<<ln;
}
INPUT:
Enter the value of n 4
Enter the value of x 7
Enter the value of x 8
OUTPUT:
The largest value 7

VAIBHAV PANDEY Page 3


C++ Programming Class XI & XII 2013

Q.5 Write a c++program to find smallest odd number out Of 'n'


number?
DATE:5|12|11
FILENAME:g5.cpp

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,c,son=0,x=0;
cout<<"Enter the value of n";
cin>>n;
for(c=0;c<n;c++)
{
cout<<"Enter the value of x";
cin>>x;
if(x%2!=0)
{if(x<son)
son=x;
cout<<"The smallest odd number is"<<son;
}
}
}
INPUT:
Enter the value of n 5
Enter the value of x 4
Enter the value of x 3
OUTPUT:
The smallest odd number is 0
**********************************************
FLOW OF CONTROL

VAIBHAV PANDEY Page 4


C++ Programming Class XI & XII 2013

Q.1 Program to accept three integers and print the largest of the
three.Make use of only if statement.
DATE:5|12|11
FILENAME:f1.cpp

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int x,y,z,max;
cout<<"Enter three numbers";
cin>>x>>y>>z;
max=x;
if(y>max)
max=y;
if(z>max)
max=z;
cout<<"\n"<<"The largestof"<<x<<","<<y<<"and"<<z<<"is"<<max;
return 0;
}
INPUT:
Enter three numbers:3 5 6
OUTPUT:
The largest of 3,5 and 6 is 6

Q.2 Temperature-conversion program that gives the user the


option of converting Fahrenheit to Celsius or Celsius to
Fahrenheit and depending upon user's choice carries out the
conversion.
DATE:5|12|11
FILENAME:f2.cpp

#include<iostream.h>

VAIBHAV PANDEY Page 5


C++ Programming Class XI & XII 2013

#include<conio.h>
int main()
{
int choice;
double temp,conv_temp;
cout<<"Temperature conversion menu"<<"\n";
cout<<"1.Farenheite to Celsius"<<"\n";
cout<<"Enter your choice(1-2):";
cin>>choice;
if(choice==1){
cout<<"\n"<<"Enter temperature in fahrenheit:";
cin>>temp;
conv_temp=(temp-32)/1.8;
cout<<"The temperature in Celsius is" << conv _temp
<<\n;
}
else
{
cout<<"\n"<<"Enter temperature in celsius:";
cin>>temp;
conv_temp=(1.8*temp)+32;
cout<<"The temperature in Fahrenheit is"<<conv_temp
<<\n;
}
return 0;
}
INPUT:
Temperature Conversion Menu
1. Fahrenheit to Celsius
2. Celsius to Fahrenheit
Enter your choice(1-2): 1
Enter temperature in Fahrenheit: 98.4
OUTPUT:
The temperature in celsius is 36.888889

VAIBHAV PANDEY Page 6


C++ Programming Class XI & XII 2013

Q.3 Program to create the equivalent of a four-function calculator


and display the result.
DATE:5|12|11
FILENAME:f3.cpp

#include<iostream.h>
int main()
{
char ch;
float a,b,result;
cout<<"Enter two number:";
cin>>a>>b;
cout<<"\n"<<"Enter the operator(+,-,*,/):";

cin>>ch;
cout<<"\n";
if(ch=='-')
result=a-b;
else
if(ch=='*')
result=a*b;
else
if(ch=='+')
result=a+b;
else
if(ch=='/')
result=a/b; //b must not be zero
else
cout<<"Wrong operator\n";
cout<<"\n"<<"The calculated result is:"<<result<<"\n";
return 0;
}
INPUT:
Enter two numbers: 7 3
Enter the operator(+,-,*,/): *

VAIBHAV PANDEY Page 7


C++ Programming Class XI & XII 2013

OUTPUT:
The calculated result is: 21

Q.4 Program to input a character and to print whether a given


character is an alphabet,digit or any other charater.
DATE:5|12|11
FILENAME:f4.cpp

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
char ch;
cout<<"Enter a character:";
cin>>ch;
if(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z')))
cout<<"You entered an alphabat"<<"\n";
else
if(ch>='0'&&ch<='9')
cout<<"You entered a digit"<<"\n";
else
cout<<"You entered acharacter other than"<<"alphabets and digits"<<"\n";
return 0;
}
INPUT:
Enter a character: 7
OUTPUT:
You entered a digit

Q.5 Program to calculate commission for the salesman.


DATE:5|12|11
FILENAME:f5.cpp

VAIBHAV PANDEY Page 8


C++ Programming Class XI & XII 2013

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float sales,comm;
cout<<"Enter sales made by the salesman:";
cin>>sales;
if(sales>5000)
if(sales>12000)
if(sales>22000)
if(sales>30000)
comm=sales*0.15;
else
comm=sales*0.10;
else
comm=sales*0.07;
else
comm=sales*0.03;
else
comm=0;
cout<<"\n"<<"The commision is:"<<comm<<"\n";
return 0;
}
INPUT:
Enter sales made by the salesman: 20000
OUTPUT:
The commission is: 1400

Q.6 Program to print whether a given character is an uppercase or


a lowercase character or a digit or any other character.Use
ASCII codes for it.
DATE:5|12|11
FILENAME:f6.cpp

VAIBHAV PANDEY Page 9


C++ Programming Class XI & XII 2013

#include<iostream.h>
#include<conio.h>
int main()
{
char ch;
cout<<"\nEnter a character:";
cin>>ch;
if(ch>=65&&ch<=90)
cout<<"You entered an uppercase character";
else if(ch>=97&&ch<=122)
cout<<"\n"<<"You entered a lowercase character:";
else cout<<"\n"<<"You entered aspecial character";
return 0;
}
INPUT:
Enter a character: 5
OUTPUT:
You entered a digit

Q.7 Program to calculate and print roots of a quadratic equation


(ax^2)+bx+c=0 (a!=0).
DATE:5|12|11
FILENAME:f7.cpp

#include<iostream.h>
#include<math.h>
#include<conio.h>
int main(){
float a,b,c,root1,root2,delta;
cout<<"Enter three number a,b & cof"<<"ax^2+bx+c:\n";
cin>>a>>b>>c;
if(!a)
cout<<"Value of\'a\' should not be zero"<<"\n aboriting!!!!!!"<<"\n";
else
{

VAIBHAV PANDEY Page 10


C++ Programming Class XI & XII 2013

delta=b*b-4*a*c;
if(delta>0)
{
root1=(-b+sqrt(delta))/(2*a);
root2=(-b-sqrt(delta))/(2*a);
cout<<"Root are real and unequal"<<"\n";
cout<<"Root1="<<root1<<"\n";
cout<<"Root2="<<root2<<"\n";
}
else if(delta==0)
{
root1=-b/(2*a);
cout<<"Roots are real and equal"<<"\n";
cout<<"Root1="<<root1;
cout<<"Root2="<<root2<<"\n";
}
else
cout<<"Roots are COMPLEX and IMAGINARY"<<"\n";
}
return 0;
}
INPUT:
Enter three numbers a , b & c of (ax^2)+bx+c: 2 3 4
OUTPUT:
Roots are COMPLEX and IMAGINARY

Q.8 Program to input number of week's day(1-7)and translate to its


equivalentname of the day of the week.(e.g.,1 to sunday,2 to
monday.....).
DATE:5|12|11
FILENAME:f8.cpp

#include<iostream.h>
#include<conio.h>
int main()

VAIBHAV PANDEY Page 11


C++ Programming Class XI & XII 2013

{
int dow;
cout<<"Enter number of week's day(1-7):";
cin>>dow;
switch(dow)
{
case1:cout<<"\n"<<"Sunday";
break;
case2:cout<<"\n"<<"Monday";
break;
case3:cout<<"\n"<<"Tuesday";
break;
case4:cout<<"\n"<<"Wednesday:";
break;
case5:cout<<"\n"<<"Thursday";
break;
case6:cout<<"\n"<<"Friday";
break;
case7:cout<<"\n"<<"Saturday";
break;
defalut:cout<<"\n"<<"Wrong number of day";
break;
}
return 0;
}
INPUT:
Enter number of week's day(1-7): 2
OUTPUT:
Monday

Q.9 Program to calculate area of a circle, a rectangle or a triangle


depending upon user's choice.
DATE:5|12|11
FILENAME:f9.cpp

VAIBHAV PANDEY Page 12


C++ Programming Class XI & XII 2013

#include<iostream.h>
#include<math.h>
int main()
{ float area , rad , len , bre , a , b , c , s ;
int ch;
cout<<"Area Menu"<<"\n";
cout<<"1.Circle"<<"\n";
cout<<"2.Rectangle"<<"\n";
cout<<"3.Triangle"<<"\n";
cout<<"Enter your choice:";
cin>>ch;
cout<<"\n";
switch(ch)
{ case 1:cout<<"Enter radius of the circle:";
cin>>rad;
area=3.14*rad*rad;
cout<<"\n"<<"The area of the circle is"<<area<<"\n";
break;
case 2:cout<<"Enter lenght and breadth of the rectangle:";
cin>>len>>bre;
area=len*bre;
cout<<"\n"<<"The area of rectangle is<<area;
break;
case 3:cout<<"Enter three sides of triangle:";
cin>>a>>b>>c;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"\n"<<"The area of triangle is<<area;
break;
case 4:cout<<"Wrong choice!!!!!!";
break;
}
return 0;
}
INPUT:
Area Menu

VAIBHAV PANDEY Page 13


C++ Programming Class XI & XII 2013

1.Circle
2.Rectangle
3.Triangle
Enter your choice: 2
Enter lenght abd breadth of rectangle: 4 6
OUTPUT:
The area of rectangle is 24

Q.10 Program to perform arithmetic calculation using switch.This


program inputs two operands and an operator and then
displays the calculated results.
DATE:5|12|11
FILENAME:f10.cpp

#include<iostream.h>
int main()
{
float op1,op2,res;
char ch;
cout<<"Enter two numbers:";
cin>>op1>>op2;
cout<<"\n"<<"Enter an operator(+,-,*,/,%):";
cin>>ch;
cout<<"\n";
switch(ch)
{
case'+':res=op1+op2;
break;
case'-':res=op1-op2;
break;
case'*':res=op1*op2;
break;
case'/':if(op2==0)
cout<<"Divide by zero error!!!";
else

VAIBHAV PANDEY Page 14


C++ Programming Class XI & XII 2013

res=op2/op1;
break;
case'%':if(op2==0)
cout<<"Divide by zero error!!!";
else
{
int r,q;
q=op2/op1;
r=op2-(q*op1);
res=r;
}
break;
default:cout<<"\n"<<"Wrong operator!!!";
}
cout<<"The caluated result is:"<<res<<"\n";
}
INPUT:
Enter two number: 4 2
Enter an operator(=,-,*,/,%) : /
OUTPUT:
The calculated result is : 0.5
*********************************************************
ARRAY
Q1. Program to read marks of 50 students and store them under an
array.
DATE:5|12|11
FILENAME:a1.cpp

#include<iostream.h>
#inlude<conio.h>
void main()
{
clrscr();
const int size=50;
VAIBHAV PANDEY Page 15
C++ Programming Class XI & XII 2013

float marks[size];
for(int i=0;i<size;i++)
{
cout<<"enter marks of students"<<i+1<<"\n";
cin>>marks[i];
}
cout<<"\n";
for(i=0;i<size;i++)
cout<<"marks["<<i<<"]="<<marks[i]<<"\n";
}
INPUT:
Enter marks of student 1:89
Enter marks of student 2:98
Enter marks of student 2:88
OUTPUT:
marks[0]=89
marks[1]=98
marks[2]=88

Q.2 Program to read prices of 5 items in an array and the display


sum of all the prices,product of all prices and average of them.
DATE:5|12|11
FILENAME:a2.cpp

#include<iostream.h>
void main()
{ double price[5],sum,avg,prod;
sum=avg=0;
prod=1;
for(int i=0;i<5;i++)
{
cout<<"enter price of items"<<i+1<<":";
cin>>price[i];
sum+=price[i];
prod*=price[i];

VAIBHAV PANDEY Page 16


C++ Programming Class XI & XII 2013

}
avg=sum/5;
cout<<"sum of all price="<<sum<<endl;
cout<<"product of all prices="<<prod<<endl;
cout<<"average of all prices="<<avg<<endl;
}
INPUT:
Enter price of item 1:25
Enter price of item 2:50
Enter price of item 3:55
Enter price of item 4:65
Enter price of item 5:90
OUTPUT:
Sum of all prices=285
Product of all prices=401287500
Average of all prices=57

Q.3 Program to accept sales of each day of the month and print the
total sales and average sales of the month.
DATE:5|12|11
FILENAME:a3.cpp

#include<iostream.h>
void main()
{
const int size=3;
float sales[size],avg=0;total=0;
for(int i=0;i<size;i++)
{
cout<<"Enter sales made on day"<<i+1;
cin>>sales[i];
total+=sales[i];
}
avg=total/size;

VAIBHAV PANDEY Page 17


C++ Programming Class XI & XII 2013

cout<<"\nTotal sales="<<total<<"\n";
cout<<<<"\nAverage sales="<<avg;
}
INPUT:
Enter sales made on day 1:19923
Enter sales made on day 2:23332
Enter sales made on day 3:33331
OUTPUT:
Total sales=76586
Average sales=25528.66

Q.4 Program to count the number of employees earning more than


Rs. 1 lakh per annum.The monthly salaries of 100 employees
are given.
DATE:5|12|11
FILENAME:a4.cpp

#include<iostream.h>
void main()
{
const int size=100;
float sal[size],an_sal;
int count=0;
//loop to read monthly salaries of 100 employees.
for(int i=0;i<size;i++)
{
cout<<"Enter monthly salary of employee"<<i+1<<":";
cin>>sal[i];
}
//loop to count employees earning more than Rs.1 lakh/annum
for(i=0;i<size;i++)
{
an_sal=sal[i]*12;

VAIBHAV PANDEY Page 18


C++ Programming Class XI & XII 2013

if(an_sal>100000)
count++;
}
cout<<count<<"Employees out of"<<size<<"employees earning more than 1 lakh per
annum";
}
INPUT:
Enter monthly salaries of employee 1:12000
Enter monthly salaries of employee 2:10000
Enter monthly salaries of employee 3:5000
Enter monthly salaries of employee 4:8000
Enter monthly salaries of employee 5:
OUTPUT:
3 employees out of 5 employees are earning more than Rs.1 lakh per annum.

Q.5 Program to read sales of 5 salesman in 12 month


and to print total sales made by each salesman?
DATE:5|12|11
FILENAME:a5.cpp

#include<iostream DATE:5|12|11
#include<conio.h>
void main()
{
clrscr();
int sales[5][12];
int i,j;
unsigned long total;
for(i=0;i<5;i++)
{
total=0;
cout<<"Enter sales for salesman"<<i+1;
for(j=0;j<12;j++)
{
cout<<"month"<<j+1;

VAIBHAV PANDEY Page 19


C++ Programming Class XI & XII 2013

cin>>sales[i][j];
total+=sales[i][j];
}
cout<<"Total sales of a salesman"<<i+1<<"="<<total;
}
}
INPUT:
Enter sales for salesman 1
month 1 2000
month 2 3340
month 3 3333
month 4 2000
month 5 3000
month 6 2500
month 7 3900
month 8 3300
month 9 2300
month 10 2300
month 11 1200
month 12 1300
OUTPUT:
Total sales of salesman 1=30473

Q.6 Program to calculate grades of 4 students from 3 test scores.


DATE:5|12|11
FILENAME:a6.cpp

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float marks[4][3],sum,avg;
char grade[4];
int i,j;

VAIBHAV PANDEY Page 20


C++ Programming Class XI & XII 2013

for(i=0;i<4;i++)
{sum=avg=0;
cout<<"enter three scores of student"<<i+1;
for(j=0;j<3;j++)
{ cin>>marks[i][j];
sum+=marks[i][j];
}
avg=sum/3;
if(avg<45.0)
grade[i]='d';
else if(avg<60.0)
grade[i]='c';
else if(avg<75.0)
grade[i]='B';
else
grade[i]='A';
}
for(i=0;i<4;i++)
{ cout<<"student"<<i+1<<"\ttotal marks="<<marks[i][0]+marks[i][1]+marks[i][2]
<<"\tgrade="<<grade[i];
}
}
INPUT:
Enter 3 scores of student 1:78 65 46
Enter 3 scores of student 2:56 65 66
Enter 3 scores of student 3:45 56 43
OUTPUT:
Student 1 Total marks=189 Grade=B
Student 2 Total marks=187 Grade=B
Student 3 Total marks=144 Grade=A

Q.7 Program to search for a specific element in a 1-D array(LINEAR


SEARCH)
DATE:5|12|11

VAIBHAV PANDEY Page 21


C++ Programming Class XI & XII 2013

FILENAME:a7.cpp

#include<iostream.h>
void main()
{
int a[20],size,i,flag=0,num,pos;
cout<<"Enter number of element in the array:";
cin>>size;
cout<<"Enter element in asending oder:";
for(i=0;i<size;i++)
cin>>a[i];
cout<<"Enter element to be searched :";
cin>>num;
for(i=0;i<size;i++)
if(a[i]==num)
{
flag=1;
pos=i;break;
}
if(flag==0)
cout<<"Element not found";
else
cout<<"Element found at position"<<pos+1;
}
INPUT:
Enter the number of elements in the array: 5
Enter the elements of array(in ascending order):1 4 7 9 18
Enter the element to be searched for:9
OUTPUT:
Element found at position 4

Q.8 Program to replace every space in a string with a hyphen.


DATE:5|12|11
FILENAME:a8.cpp

#include<iostream.h>

VAIBHAV PANDEY Page 22


C++ Programming Class XI & XII 2013

#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char string[90];
cout<<"Enter string(max 89 character):";
cin.getline(string,90);
int x1=strlen(string);
for(int i=0;string[i]!='\o';i++)
if(string[i]==' ')
string[i]='_';
cout<<"The changed string is";
cout.write(string,x1);
}
INPUT:
Enter string(max 89 character): India is Great!!!
OUTPUT:
The changed string is India-is-Great!!!

Q.9 Program to find number of vowels in a given line of text.


DATE:5|12|11
FILENAME:a9.cpp

#include<iostream.h>
#include<stdio.h> //for gets()
void main()
{
char line[90];
int vow_ctr=0;
cout<<"Enter the line:";
gets(line); //gets() is used to read strings
for(int i=0;line[i]!='\0';i++)
{

VAIBHAV PANDEY Page 23


C++ Programming Class XI & XII 2013

switch(line[i])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
++vow_ctr;
}
}
cout<<vow_ctr;
}
INPUT:
Enter the line:
God is Great.
OUTPUT:
The total number of vowels in the given line is 4

Q10. Program to read and check the equality of two matrices.


DATE:5|12|11
FILENAME:a10.cpp

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int A[3][3],B[3][3],r,c;
cout<<"Enter first matrix row wise";
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)

VAIBHAV PANDEY Page 24


C++ Programming Class XI & XII 2013

{
cin>>A[r][c];
}
}
cout<<"Enter second matrix row wise";
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{
cin>>B[r][c];
}
}
int flag 0;
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{
if(A[r][c]!=B[r][c])
{
flag=1; break;
}
}
if(flag==1)break;
}
if(flag)
cout<<"Matrix are unequal";
else
cout<<"Matrix are equal";
}
INPUT:
Enter first matrix row wise
123
456
789
Enter second matrix row wise
123

VAIBHAV PANDEY Page 25


C++ Programming Class XI & XII 2013

456
789

OUTPUT:
Matrix are equal

******************************************************
USER DEFINED FUNCTION
Q.1 Program to print cube of a given number using a function.
DATE:6|12|11
FILENAME:u1.cpp

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float cube(float);
float x,y;
cout<<"enter number whose cube is calculated:";
cin>>x;
y=cube(x);
cout<<"the cube of"<<x<<"is"<<y;
getch ();
return 0;
}
float cube(float a)
{
float n;
n=a*a*a;
return n;
}
INPUT:
Enter number whose cube is to be calculated: 3
OUTPUT:

VAIBHAV PANDEY Page 26


C++ Programming Class XI & XII 2013

The cube of 3 is 27

Q.2 Program to illustrate the call by value method of function


invoking.
DATE:6|12|11
FILENAME:u2.cpp

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int change(int);
int orig=10;
cout<<"the original value is:";
cout<<"the return of function change() is"<<change(orig);
cout<<"the value after function change() is over"<<orig;
getch ();
}
int change(int a)
{
a=20;
return a ;
}
OUTPUT:
The original value is 10
Return value of function change() is 20
The value after function change() is over 10

Q.3 Program to show handicap of call by value method.


DATE:6|12|11
FILENAME:u3.cpp

#include<iostream.h>

VAIBHAV PANDEY Page 27


C++ Programming Class XI & XII 2013

#include<conio.h>
int main()
{
clrscr();
void swap(int,int);
int a=7 ,b=4;
cout<<"original value are:";
cout<<"a="<<a<<",b="<<b;
swap(a,b);
cout<<"the value aftre swap() are:";
cout<<"a="<<a<<",b="<<b;
getch ();
return 0;
}
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"swapped values are:";
cout<<"a="<<x<<",b="<<y;
}
OUTPUT:
Original values are: a=7 , b=4
Swapped values are: a=4 , b=7
The values after swap() are: a=7 , b=4
Q.4 Program to swap two values.
DATE:6|12|11
FILENAME:u4.cpp

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();

VAIBHAV PANDEY Page 28


C++ Programming Class XI & XII 2013

void swap(int&,int&);
int a=7 ,b=4;
cout<<"original value are:";
cout<<"a="<<a<<",b="<<b;
swap(a,b);
cout<<"the value after swap() are:";
cout<<"a="<<a<<",b="<<b;
getch ();
return 0;
}
void swap(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"swapped values are:";
cout<<"a="<<x<<",b="<<y;
}
OUTPUT:
The original values are: a=7 , b=4
The swapped values are: a=4 , b=7
The values after swap() are: a=4 , b=7

Q.5 Program to convert distance in feet or inches using a call by


reference method.
DATE:6|12|11
FILENAME:u5.cpp

#include<iostream.h>
#include<conio.h>
#include<process.h>
int main()
{
clrscr();

VAIBHAV PANDEY Page 29


C++ Programming Class XI & XII 2013

void convert(float &,char&,char);


float distance;
char choice,type='F';
cout<<"Enter the distance in feet:";
cin>>distance;
cout<<"You want distance in feet or inches?(F/I):";
cin>>choice;
switch(choice)
{
case'F':convert(distance,type,'F');
break;
case'I':convert(distance,type,'I');
break;
default:cout<<"you have entered a wrong choice!!!";
exit(0);
}
cout<<"distance="<<distance<<""<<type;
getch ();
return 0;
}
void convert(float &d,char &t,char ch)
{
switch(ch)
{
case'F':if(t=='I')
{
d=d/12;
t='F';
}
break;
}
return ;
}
INPUT:
Enter distance in feet: 15
You want distance in feets or inches?(F/I): I

VAIBHAV PANDEY Page 30


C++ Programming Class XI & XII 2013

OUTPUT:
Distance=180 I

Q.6 Program to check whether a given character is contained in a


string or not and find its position.
DATE:6|12|11
FILENAME:u6.cpp

#include<iostream.h><
#include<conio.h>
int main()
{
clrscr();
int findpos(char s[],char c);
char string[80],ch;
int y=0;
cout<<"Enter the main string:";
cin.getline(string,80);
cout<<"Enter character to be searched for:";
cin.get(ch) ;
y=findpos(string,ch);
if(y==-1)
cout<<"sorry!character is not in the string:";
getch ();
return 0;
}
int findpos(char s[],char c)
{
int flag=-1;
for(int i=0;s[i]!='\0';i++)
{ if(s[i]==c)
{
flag=0;
cout<<"the character is in the string at position"<<i+1;
}

VAIBHAV PANDEY Page 31


C++ Programming Class XI & XII 2013

}
return(flag);
}
INPUT:
Enter main string: India is Great
Enter character to be searched for: a
OUTPUT:
The character is in the string at position 5
The character is in the string at position 13

Q.7 Write a C++ program that invokes a function calc() which


intakes two integers and an arithmetic operator and prints the
corresponding result.
DATE:6|12|11
FILENAME:u7.cpp

#include<iostream.h>
#include<conio.h>
#include<process.h>
int main()
{
clrscr();
void calc(int,int,char);
int a,b;
char ch;
cout<<"Enter the two integers:";
cin>>a>>b;
cout<<"Enter the arithmatic operator (+,-,*,/):";
cin>>ch;
calc(a,b,ch);
return 0;
}
void calc(int x,int y,char z)
{
switch(z)

VAIBHAV PANDEY Page 32


C++ Programming Class XI & XII 2013

{
case'+':cout<<"The sum of "<<x<< "and" <<y<<" is"
<<(x+y);
break;
case'-':cout<<"The subraction of "<<x<<"and"<<y<<"is"
<<(x-y);
break;
case'*':cout<<"Product of"<<x<<"and"<<y<<"is"
<<(x*y);
break;
case'/':if(x<y)
{ cout<<"First integer should be"
<<"greater than second.";
exit(0);
}
cout<<"Quotient: <<x<<"/"<<y<<"is"<<(x/y);
break;
default:cout<<"wrong operator!!";
break;
}
return;
}
INPUT:
Enter two integers:2 3
Enter arithmetic operator(+,-,*,/): +
OUTPUT:
The sum of 2 and 3 is 5

Q.8 Write a function that normally takes one argument,the address


of a string,and prints the string once.However,if a second,type
int argument is provided and is non zero and other than 1,the
function prints the string a number of time that function has
been called to that point.
DATE:6|12|11

VAIBHAV PANDEY Page 33


C++ Programming Class XI & XII 2013

FILENAME:u8.cpp

#include<iostream.h>
#include<conio.h>
void prnstr(char*s,int n=1);
void main()
{
clrscr();
char s[]="PACE";
cout<<"1st call with 1 argument:"; //only one argument passed
prnstr(s);
cout<<"2nd call with 2 arguments:"; //two argument passed
prnstr(s,3);
cout<<"3rd call with 1 arguments :"; //two argument passed
prnstr(s);
cout<<"4th call with 1 argument:"; //two argument passed
prnstr(s,3);
}
void prnstr(char*s,int n)
{ static int count=1;
int i=0,j=0;
if(n==1)
{
for(;s[i];i++)
cout<<s[i];
cout<<endl;
}
else if(n>1)
{
for(;j<count;j++)
{
for(i=0;s[i];i++)
cout<<s[i];
cout<<endl;
}
}

VAIBHAV PANDEY Page 34


C++ Programming Class XI & XII 2013

count++; //static variable incremented


}
OUTPUT:
1st call with 1 argument PACE
2nd call with 2 argument PACE PACE
3rd call with 1 argument PACE
4th call with 1 argument PACE PACE PACE PACE

Q.9 Write a complete C++ program that reads a float array having 5
element.The program uses a function reverse() to reverse this
array.Make suitable assumptions wherever required.
DATE:6|12|11
FILENAME:u9.cpp

#include<iostream.h>
#include<conio.h>
void reverse(float array[15]);
void main()
{ clrscr();
float arr[5];
for(int i=0;i<5;i++)
cin>>arr[i];
reverse(arr);
}
void reverse(float array[5])
{
for(int i=4;i>=0;--i)
cout<<array[i];
}
INPUT:1 2 3 4 5
OUTPUT:5 4 3 2 1
***************************************************
OPERATORS AND EXPRESSIONS

VAIBHAV PANDEY Page 35


C++ Programming Class XI & XII 2013

Q.1 Write a C++ program that inputs experience and age of a


person.The salary of the person is 6000 if the person is
experienced and his age is more than 35,otherwise if the
person is experienced and his age is more than 28 but less than
35 than the salary be 4800 otherwise for experienced peron
the salary should be 3000 and for inexperienced person the
salary should be 2000.
DATE:6|12|11
FILENAME:o1.CPP

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int exper,age,salary;
cout<<"\nIs person the experienced?(enter 1 for yes and 0 for no):";
cin>>exper;
cout<<"\nEnter age of the person:";
cin>>age;
salary=exper?((age>35)?6000:(age>28)?4800:3000)
:2000 ; cout<<"\nThe salary of the person
is:"<<salary;
}

INPUT :
Is person the experienced?(enter 1 for yes and 0 for no):1
Enter age of the person:23

OUTPUT:
Talary of the person is:3000

VAIBHAV PANDEY Page 36


C++ Programming Class XI & XII 2013

Q.2 Write a program that asks for the number of players,and then
gives the number of teams and number of players left over.
DATE:6|12|11
FILENAME:o2.cpp

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int no_of_players,no_of_teams,left_overs;
cout<<"Enter the no of players:";
cin>>no_of_players;
no_of_teams=no_of_players/5;
left_overs=no_of_players%5;
cout<<"\nThere will be "<<no_of_teams<<" teams with "<<left_overs<<" left overs";
}

INPUT :
Enter the no of players:11

OUTPUT :
There will be 2 teams with 1 left overs

Q.3 The value of e is known to be 2.71828....Using this value,write a


program to determine the value of the expression:2-
(ye^2y)+(4^y).Obtain value of y from user.
DATE:6|12|11
FILENAME:o3.cpp

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()

VAIBHAV PANDEY Page 37


C++ Programming Class XI & XII 2013

{
clrscr();
const double e=2.71828;
double result,y;
cout<<"\nEnter the value for y:";
cin>>y;
result=2-y*exp(2*y)+pow(4,y);
cout<<"\nThe result of given expression is:"<<result;
}
INPUT:
Enter the value for y:20
OUTPUT:
The result of given experession is :-4.707704e+18

Q.4 Write a program which will raise any number X to a positive


power n.Obtain values of x and n from user.
DATE:6|12|11
FILENAME:o4.cpp

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int x,n;
cout<<"\nEnter the base and exponent:";
cin>>x>>n;
cout<<"\n"<<x<<" to the power "<<n<<" is "<<pow(x,n);
}
INPUT :
Enter the bese and exponent:2 5
OUTPUT :
2 to the power 5 is 32

VAIBHAV PANDEY Page 38


C++ Programming Class XI & XII 2013

Q.5 Write a C++ program to input a number.If number is even,print


square otherwise print cube.
DATE:6|12|11
FILENAME:o5.cpp

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int n;
cout<<"\nEnter the number:";
cin>>n;
if(n%2==0)
cout<<"\nThe number is even and its square is "<<pow(n,2);
else
cout<<"\nThe number is odd and its cube is "<<pow(n,3);
}
INPUT :
Enter the number:5
OUTPUT :
THE number is odd and its cube is 125

Q.6 Write a C++ program to input a number.If number n is odd and


positive,print its square root otherwisw print n^5.
DATE:6|12|11
FILENAME:o6.cpp

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()

VAIBHAV PANDEY Page 39


C++ Programming Class XI & XII 2013

{
clrscr();
int n;
float y;
cout<<"\nEnter the number:";
cin>>n;
if((n%2!=0)&&(x>0))
y=sqrt(n);
else
y=pow(n,5);
cout<<y;
}
INPUT:
Enter the number: 5
OUTPUT:
3125

Q.7 Write a C++ program to input choice(1 or 2).If choice is 1,print


area of circle otherwise print its perimeter.
DATE:6|12|11
FILENAME:o7.cpp

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int r,ch;
cout<<"Enter the radius:";
cin>>r;
cout<<"\nEnter the choice:";
cout<<"\n1.Area of circle";
cout<<"\n2.Perimeter of circle";
cin>>ch;

VAIBHAV PANDEY Page 40


C++ Programming Class XI & XII 2013

if(ch==1)
cout<<"\nArea of circle of radius "<<r<<" is "<<3.14*pow(r,2);
else
cout<<"\nPerimeter of circle of radius "<<r<<" is "<<2*3.14*r;
}
INPUT :
Enter the radius: 5
Enter your choice:
1.Area of circle
2.perimeter of circle1
OUTPUT :
Area of circle of radius 5 is 78.5

Q.8 Write a C++ program to input three integers and print largest
of three.
DATE:6|12|11
FILENAME:o8.cpp

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,l=0;
cout<<"Enter three numbers:";
cin>>a>>b>>c;
if(a>l)
l=a;
if(b>l)
l=b;
if(c>l)
l=c;
cout<<"\nLargest no out of "<<a<<","<<b<<","<<c<<" is "<<l;
}
INPUT :enter three number 5 7 8

VAIBHAV PANDEY Page 41


C++ Programming Class XI & XII 2013

OUTPUT :largest of three number is 8

Q.9 Write a C++ progam to input a student type(A or B).If the


student type is'A'initialize college account with Rs. 200/-
otherwise initialise the hostel account with Rs. 200/-.
DATE:6|12|11
FILENAME:o9.cpp

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int c_a=0,h_a=0;
char type;
cout<<"\nEnter type as A or B:";
cin>>type;
if(type=='A'||type=='a')
cout<<"\nCollege account="<<c_a+200;
if(type=='B'||type=='b')
cout<<"\nHostel account="<<h_a+200;
}
INPUT :
enter as Aor B: A
OUTPUT:
College amount=200

Q.10 Write a program that reads in a character <char> from he


keyboard and then displays one of the following messages:
(i) If<char>is a lower case letter,the message "The upper case
character corresponding to<char> is....",
(ii)If<char> is an uppercase letter,the message "The lower case
character corresponding to<char> is....",
VAIBHAV PANDEY Page 42
C++ Programming Class XI & XII 2013

(iii)If<char> is not a letter,the message<char>is not a letter".


DATE:6|12|11
FILENAME:o10.cpp

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
cout<<"\nEnter a character:";
cin>>ch;
if((ch>=65)&&(ch<=90))
cout<<"\nThe lowercase character corresponding to "<<ch<<" is "<<(char)(ch+32);
else if((ch>=97)&&(ch<=122))
cout<<"\nThe uppercase character corresponding to "<<ch<<" is "<<(char)(ch-32);
else
cout<<"\nCharacter is not a letter";
}
INPUT :
enter a character:S
OUTPUT :
the lowercase character corrosponding to S is :s
******************************************************
STRUCTURES

Q.1 Progran to read values into a nested structure.


DATE:6|12|11
FILENAME:s1.cpp

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct addr //global definition

VAIBHAV PANDEY Page 43


C++ Programming Class XI & XII 2013

{
int houseno ;
char area[6];
char city[6];
char state[6];
};
struct emp
{
int empno ;
char name[6];
char desig[4];
addr address ;
float basic ;
} worker ;
void main()
{
clrscr();
cout<<"\n Enter employee number :"<<"\n";
cin>>worker.empno;
cout<<"\n"<<"name";
gets(worker.name);
cout<<"\n"<<"designation";
gets(worker.desig) ;
cout<<"\n enter address :"<<"\n";
cout<<"house no :";
cin>>worker.address.houseno;
cout<<"\n"<<"area";
gets(worker.address.area);
cout<<"\n"<<"city";
gets(worker.address.state);
cout<<"\n"<<"state :";
gets(worker.address.state) ;
cout<<"\n"<<"basic pay :";
cin>>worker.basic;
}

VAIBHAV PANDEY Page 44


C++ Programming Class XI & XII 2013

OUTPUT :
Enter employee number : 103
Name:neel
Designation:Manager
Enter address:
house no:321
Area:Sadiq,Nagar
City:delhi
State:Delhi
Basic Pay:9500

Q.2 Program to illustrate passing of structures by value.


DATE:6|12|11
FILENAME:s2.cpp

#include<iostream.h>
#include<conio.h>
struct distance
{
int feet;
int inches;
};
int main()
{
clrscr;
distance length1,length2;
void prnsum(distance & l1,distance & l2);
cout<<"Enter length 1:"<<"\n";
cout<<"feet:";
cin>>length1.feet;
cout<<"\n\n Enter length2:"<<"\n";
cout<<"\n"<<"inches:";
cin>>length2.inches;
prnsum(length1,length2);
return 0;
}

VAIBHAV PANDEY Page 45


C++ Programming Class XI & XII 2013

void prnsum(distance & l1,distance & l2)


{
distance l3;
l3.feet = l1.feet+l2.feet+(l1.inches+l2.inches)/12;
l3.inches=(l1.inches+l2.inches)%12;
cout<<"\n\nTotal feet:"<<l3.feet<<"\n";
cout<<"Total inches:"<<l3.inches;
return;
}
INPUT:
Enter lenght 1:
feet:6
inches:4
Enter lenght 2:
feet:4
inches:5

OUTPUT:
Total feet:10
Total inches:9

Q.3 Program to display labels of information stored in a structure.


DATE:6|12|11
FILENAME:s3.cpp

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<iomanip.h>
#define maxlabels 8
struct Person
{
char title[3];
char firstname[15];
char lastname[15];

VAIBHAV PANDEY Page 46


C++ Programming Class XI & XII 2013

char address1[30];
char address2[25];
char city[25];
char state[25];
long pin;
};
void main()
{
Person labels[30];
int lab_per_page;
for(int i=0;i<30;i++)
{
cout<<"\n Enter information for person"<<i+1<<":"<<endl;
cout<<"\n Title(mr./ms.):";
cin>>labels[i].title;
cout<<"\n First name:";
cin>>labels[i].firstname;
cout<<"\n Last name";
cin>>labels[i].lastname;
cout<<"\n House no, street:";
gets(labels[i].address1);
cout<<"\n Area:";
gets(labels[i].address2);
cout<<"\n City:";
gets(labels[i].city);
cout<<"\n State:";
gets(labels[i].state);
cout<<"\n Pin:";
cin>>labels[i].pin;
}
cout<<"\n Enter how many labels you want to display per page:";
cin>>lab_per_page;
if(lab_per_page>maxlabels)
lab_per_page=maxlabels;
int p=1,count=0;
char name[30],ctState[50];

VAIBHAV PANDEY Page 47


C++ Programming Class XI & XII 2013

cout<<"\n\t\t PAGE"<<p<<endl;
cout.setf(ios::left);
for(i=0;i<30;)
{
strcpy(name,labels[i].firstname); strcat(name," ");
strcat(name,labels[i].lastname);
cout<<labels[i].title<<" "<<setw(30)<<name<<"\t";
strcpy(name,labels[i+1].firstname); strcat(name," ");
strcat(name,labels[i+1].lastname);
cout<<labels[i+1].title<<" "<<name<<endl;
cout<<setw(30)<<labels[i].address1<<"\t"<<setw(30)<<labels[i+1].address1<<endl;
cout<<setw(30)<<labels[i].address2<<"\t"<<setw(30)<<labels[i+1].address2<<endl;
strcpy(ctState,labels[i].city);
strcat(ctState,"(");
strcat(ctState,labels[i].state);
strcat(ctState,")");
cout<<setw(30)<<ctState<<"\t"<<labels[i+1].city<<"("<<labels[i+1].state<<")"<<endl;
cout<<setw(30)<<labels[i].pin<<"\t"<<labels[i+1].pin<<endl;
cout<<endl;
i+=2;
count+=2;
if(count==8)
{
p++;count=0;
cout<<"\n\t\t PAGE"<<p<<endl;
}
}
}

INPUT:
Enter information for person 1
Title(mr./mrs):mr.
First name:mayank
last name:singh
house no,street:123
area:j.k colony

VAIBHAV PANDEY Page 48


C++ Programming Class XI & XII 2013

city:gotan
State:rajasthan
pincode:342902

Enter information for person 2


Title(mr./mrs):mr.
First name:sajjan
last name:gehlot
house no,street:321
area:j.k colony
city:gotan
State:rajasthan
pincode:342902
Enter how many labels you want to display perpage 2

OUTPUT:
mr. abhishek agarwal mr. somesh gupta
123,j.k colony 321,j.k colony
gotan,rajasthan, gotan,rajasthan,
342902 342902

Q.4. Define and initialize a table of the names of months of the years
and number of days in each months,using an array of structure
holding the name of a month and the number of days in it.
DATE:6|12|11
FILENAME:s4.cpp

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct year
{
char month[12];
int days;
};

VAIBHAV PANDEY Page 49


C++ Programming Class XI & XII 2013

year table[12]={
{"january",31},
{"february",28},
{"march",31},
{"april",30},
{"may",31},
{"june",30},
{"july",31},
{"august",31},
{"september",30},
{"october",31},
{"november",30},
{"december",31}
};
**********************************************
STANDARD LIBRARY FUNCTION

Q.1 Write a program that checks whether the given character is


alphanumeric or a digit.
DATE:6|12|11
FILENAME:st1.cpp

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
void main()
{
clrscr();
char ch;
int a;
cout<<"\n Enter the character:\n";
cin>>ch;
a=ch;

VAIBHAV PANDEY Page 50


C++ Programming Class XI & XII 2013

if(isalnum(a))
{
cout<<"\n Albhabetic chracter\n";
if(isdigit(a))
cout<<"\n And digit charcter\n";
else
if(isalpha(a))
cout<<"\n And alphabetic chracter::\n";
}
else
cout<<"\n Other character\n";
getch();
}

INPUT :
enter the character:r
OUTPUT :
Alphabetic character And alphabetic character::

Q.2 Write a C++ program that checks whether a given character is


an alphabet or not.If it is an alphabet,whether it is lowercase or
uppercase alphabet.
DATE:6|12|11
FILENAME:st2.cpp

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
void main()
{
clrscr();
char ch;
cout<<"\nEnter the character:\n";

VAIBHAV PANDEY Page 51


C++ Programming Class XI & XII 2013

cin>>ch;
if(isalpha(ch))
{ if (islower(ch))
cout<<"\nLowercase alphabet\n";
else
if(isupper(ch))
cout<<"\n Uppercase alphabet\n";
}
else
cout<<"\n other character\n";
getch();
}
INPUT:
Enter the character :a
OUTPUT:
Lowercase alaphabet.

Q.3. Write a program that reads a string and converts it to


uppercase.
DATE:6|12|11
FILENAME:st3.cpp

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void main()
{ clrscr();
char str[25];
int flag=1;
cout<<"\n enter the string\n";
cin.getline(str,25);
for(int i=0;str[i]!='\0';i++)
{
if(isupper(str[i]))

VAIBHAV PANDEY Page 52


C++ Programming Class XI & XII 2013

{
flag=0;
cout<<str[i]<<"is already in uppercase\n";
}
else if(islower(str[i]))
{
flag=1;
str[i]=toupper(str[i]);
}
}
if((flag==1)||(str[i]=='\0'))
{
cout<<"uppercase string is:\n";
int x1=strlen(str);
cout.write(str,x1);
}
}
INPUT:
enter the string:
god is great
OUTPUT:
Uppercase string is:
GOD IS GREAT

Q.4 Write a C++ program that reads two strings and appends the
first to the second.
DATE:6|12|11
FILENAME:st4.cpp

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main ()
{
clrscr();

VAIBHAV PANDEY Page 53


C++ Programming Class XI & XII 2013

char str1[25],str2[50];
cout<<"\n Enter first string(max 25 character)\n";
cin.getline(str1,25);
cout<<"\n Enter second string(max 25 character)\n";
cin.getline(str2,50);
strcat(str2,str1);
int len=strlen(str2);
cout.write(str2,len);
}

INPUT :
enter first string:somnath and
enter second string:rakesh is in class

OUTPUT :
Somnath and rakesh is in class

Q.5 Write a C++ program that reads ntwo string and copies the
smaller string into bigger string.
DATE:6|12|11
FILENAME:st5.cpp

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void main()
{ clrscr();
char str1[25],str2[25];
int len1,len2;
cout<<"\n Enter first string";
cin.getline(str1,25);
cout<<"\nEnter second string";
cin.getline(str2,25);

VAIBHAV PANDEY Page 54


C++ Programming Class XI & XII 2013

if(strlen(str1)>strlen(str2))
{
strcpy(str1,str2);
cout<<"\n second string is copied to first string\n";
len1=strlen(str1);
cout.write(str1,len1);
}
else if(strlen(str2)>strlen(str1))
{
strcpy(str2,str1);
cout<<"\n First string is copied to second string\n";
len2=strlen(str2);
cout.write(str2,len2);
}
else if(strlen(str2)==strlen(str1))
{
cout<<"\n Strings are equal\n";
len1=strlen(str1);
cout<<"\nString 1 is";
cout.write(str1,len1);
cout<<"\nstring2 is :";
len2=strlen(str2);
cout.write(str2,len2);
}
}
INPUT:
Enter first string: Rakesh
Enter second string: Mayank
OUTPUT:
First string is copied to second string: Rakesh

Q.6 Write a C++ program that converts lower case letters in given
string to corresponding uppercase letter and vice versa.

VAIBHAV PANDEY Page 55


C++ Programming Class XI & XII 2013

DATE:6|12|11
FILENAME:st6.cpp

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void main()
{
clrscr();
char str[10];
cout<<" enter a string:\n";
cin.getline(str,10);

for(int i=0;str[i]!='\0';i++)
{
if(isupper(str[i]))
str[i]=tolower(str[i]);
else
if (islower(str[i]))
str[i]=toupper(str[i]);
}
int x=strlen(str);
cout.write(str,x);
}

INPUT :
enter a string:love the way u lie

OUTPUT
LOVE THE WAY U LIE
*********************************************************
CLASS XII
CLASS AND OBJECTS

VAIBHAV PANDEY Page 56


C++ Programming Class XI & XII 2013

Q1. WAP Program to illustrate functioning of scope resolution


operator::?
Date : 04/12/11
File name : b1.cpp

#include<iostream.h>
#include<conio.h>
int a=10; //global a with file scope.
void main()
{
int a=15; //a redeclared, local to main()
cout<<"main()"<<"\n";
cout<<"a="<<a<<"\n"; //local a(=15) printed.
cout<<"::a="<<::<<"\n"; //global a(=10) printed.
{ //inner block within main()
int a=24; //a again declared, local to inner block
cout<<"inner block\n";
cout<<"a="<<a<<"\n"; //local a(=25) printed.
cout<<"::a"<<::a<<"\n"; //global a(=10) printed.
}
cout<<"back to main menu()"<<"\n";
cout<<"a="<<a<<"\n"; //local a(=15) printed
cout<<"::a"<<::a<<"\n"; //global a(=10) printed
}

OUTPUT:

Main ()
a = 15
::a = 10
Inner Block
a = 25
::a 10
Back to main ()
a = 15

VAIBHAV PANDEY Page 57


C++ Programming Class XI & XII 2013

::a 10
Q2. WAP Program to illustrate the use of object arrays by
storing details of 10 items in an array of objects?
Date : 04/12/11
File name : b2.cpp

#include<iostream.h>
class Item
{
int itemno;
float price;
public:
void getdata(int i,float j)
{
itemno=i;
price=j;
}
void putdata(void)
{
cout<<"itemno:"<<itemno<<"\t";
cout<<"price:"<<price<<"\n";
}
};
const int size=10;
Item order[size];
int main()
{
int ino;
float cost;
for(int a=0;a<size;a++)
{
cout<<"enter itemno & price for item"<<a+1<<"\n";
cin>>ino>>cost;
order[a].getdata(ino,cost); //invoke getdata() for a particular
} //object with the given values

VAIBHAV PANDEY Page 58


C++ Programming Class XI & XII 2013

//print details
for(a=0;a<size;a++)
{
cout<<"item"<<a+1<<"\n";
order[a].putdata(); //invoke putdata() for a particular object
}
return 0;
}

Output

Sample program for three item (After entering data for 5 items)
Item 1
Itemno :101 Price :150
Item 2
Itemno :120 Price :110
Item 3
Itemno :125 Price :250

Q3. WAP Program to illustrate the working of a function returning


an object.
Date : 04/12/11
File name : b3.cpp

#include<iostream.h>
#include<conio.h>
class distance
{
int feet,inches;
public:
void getdata(int f,int i)
{
feet=f;
inches=i;
}
VAIBHAV PANDEY Page 59
C++ Programming Class XI & XII 2013

void printit(void)
{
cout<<feet<<"feet"<<inches<<"inches"<<"\n";
}
distance sum(distance d2); //add Distance d2 to current Distance object
};
distance distance::sum(distance d2)
{
distance d3; //create object d3 of type Distance
d3.feet=feet+d2.feet+(inches+d2.inches)/12;
d3.inches=(inches+d2.inches)%12;
return (d3);
}
int main()
{
clrscr();
distance length1,length2,total;
length1.getdata(17,6);
length2.getdata(13,8);
total=length1.sum(length2); //i.e., total=Length1+Length2
cout<<"length1";
length1.printit();
cout<<"lenth2:";
length2.printit();
cout<<"total length:";
total.printit();
return 0;
}

INPUT :
Length : 17 feet 6 inches
Length : 13 feet 8 inches

OUTPUT :
Total Length : 31 feet 2 inches

VAIBHAV PANDEY Page 60


C++ Programming Class XI & XII 2013

Q4. WAP Program to keep count of created objects using static


members?
Date : 04/12/11
File name : b4.cpp

#include<iostream.h>
#include<conio.h>
class X {
int codeno;
float price;
static int count; //static data member
public:
void getval(int i,float j)
{
codeno=i;
price=j;
++count;
}
void display(void)
{
cout<<"codeno:"<<codeno<<"\t";
cout<<"price"<<price<<"\n";
}
static void dispcount(void) //static member function
{ cout<<"count="<<count<<"\n";
}
};
int X::count=0;
int main()
{
X ob1,ob2;
ob1.getval(101,56.9);
ob2.getval(109,89.7);
X::dispcount(); //invoke static funtion

VAIBHAV PANDEY Page 61


C++ Programming Class XI & XII 2013

X ob3;
ob3.getval(109,90.8);
X::dispcount();
ob1.display();
ob2.display();
ob3.display();
return 0;
}

INPUT:
Count = 2
Count = 3
OUTPUT:
Code no :101 Price :25.12
Code no :102 Price :38.19
Code no :103 Price :49

Q5. Write the output of the following program?


Date : 04/12/11
File name :b5.cpp

#include<iostream.h>
#include<conio.h>
class counter
{private:
unsigned int count;
public:
counter() { count=0;}
void inc_count() {count++;}
int get_count() { return count;}
};
void main()
{ clrscr();
counter c1,c2;

VAIBHAV PANDEY Page 62


C++ Programming Class XI & XII 2013

cout<<"\tc1="<<c1.get_count();
cout<<"\tc2="<
c2.get_count();
c1.inc_count();
c2.inc_count();
c2.inc_count();
cout<<"\tc1="<<c1.get_count();
cout<<"\tc2="<<c2.get_count();
}
Output
C1=0 C2=0 C1=1 C2=2
**********************************************
INHERITANCE

Q1. Program to illustrate the access control in public derivation of


a class?
Date : 09/12/11
File name : e1.cpp

#include<iostream.h>
#include<stdio.h> //for gets()
const int LEN=25;
class employee {
private:
char name[LEN];
unsinged long enumb;
public:
void getdata()
{
cout<<"enter name";
gets(name);
cout<<"enter employee number:";
cin>>enumb;
}

VAIBHAV PANDEY Page 63


C++ Programming Class XI & XII 2013

void putdata()
{
cout<<"name:"<<name<<"\t";
cout<<"emp.number:"<<enumb<<"\t";
cout<<"basic salary:"<<basic;
}
protected:
float basic;
void getbasic()
{
cout<<"enter basic";
cin>>basic;
}
};
class maneger:public employee
{
private:
char title[LEN];
public:
void getdata()
{
employee::getdata(); //To resolve identity as Manager also has a get data()
getbasic();
cout<<"enter Titel:";
gets(titel);
cout<<"\n";
}
void putdata()
{
employee::putdata();
cout<<"\ttitle:"<<title<<"\n";
}
};
int main()
{ clrscr();
manager m1,m2;

VAIBHAV PANDEY Page 64


C++ Programming Class XI & XII 2013

cout<<"manager1\n"; m1.getdata();
cout<<"manager2\n"; m2.getdata();
cout<<"\t\tManager1 details\n"; m1.putdata();
cout<<"\t\tManager2 details\n"; m2.putdata();
return 0;
}

INPUT :
Manager1
Enter Name : Lubna Adams
Enter Employee Number : 1112
Enter Basic : 1180
Enter Title : Ms

Manager2
Enter Name : Uma Mahadevan
Enter Employee Number : 2031
Enter Basic : 9950
Enter Title : Ms

OUTPUT :
Manager1 Details
Name : Lubna Adams Emp. Number : 1112 Basic Salary : 11800 Title : Ms
Manager2 Details
Name : Uma Mahadevan Emp. Number : 2031 Basic Salary : 9950 Title : Ms

Q2. Program to illustrate access control of inherited members in


the privately derived class?
Date : 09/12/11
File name : e2.cpp

#include<iostream.h>
#include<stdio.h> //for gets()

VAIBHAV PANDEY Page 65


C++ Programming Class XI & XII 2013

#include<conio.h> //for clrscr()


const int LEN=25;
class employee {
private:
char name[LEN];
long enumb;
public:
void getdata()
{
cout<<"enter name";
gets(name);
cout<<"enter employee number:";
cin>>enumb;
}
void putdata()
{
cout<<"name:"<<name<<"\t";
cout<<"emp.number:"<<enumb<<"\t";
cout<<"basic salary:"<<basic;
}
protected:
float basic;
void getbasic()
{
cout<<"enter basic";
cin>>basic;
}
};
class maneger:private employee
{
private:
char title[LEN];
public:
void getdata()
{
employee::getdata(); //To resolve the identity as Manager also has a getdata()

VAIBHAV PANDEY Page 66


C++ Programming Class XI & XII 2013

getbasic();
cout<<"enter Title:";
gets(title);
cout<<"\n";
}
void putdata()
{
employee::putdata();
cout<<"\ttitle:"<<title<<"\n";
}
};
void main()
{ clrscr();
manager m1,m2;
cout<<"manager1\n";
m1.getdata();
cout<<"manager2\n";
m2.getdata();
cout<<"\t\tManager1 details\n";
m1.putdata();
cout<<"\t\tManager2 details\n";
m2.putdata();
}

INPUT :
Manager1
Enter Name : Pooja roy
Enter Employee number : 2310
Enter Basic :12700
Enter title :Mrs.

Manager2
Enter Name :Sunita ghosh
Enter Employee number :1104
Enter Basic :14500

VAIBHAV PANDEY Page 67


C++ Programming Class XI & XII 2013

Enter title :Mrs.

OUTPUT :
Manager1 Details
Name : Pooja Roy Emp. Number : 2310 Basic Salary : 12700 Title : Mrs.
Manager2 Details
Name : Priyam Emp. Number : 1104 Basic Salary : 14500 Title : Mrs.

Q3. Program to check working of constructors and destructors in


multiple inheritance?
Date : 09/12/11
File name : e3.cpp

#include<iostream.h>
#include<conio.h> //for clrscr()
class base1 { protected:
int a;
public:
base1(int x)
{a=x; cout<<"constructing base1\n";}
~base1()
{ cout<<"destructive base1 \n";}
};
class base2 { protected:
int b;
public:
base2(int y)
{ b=y;cout<<"destructive base2 \n";}
~base2()
{ cout<<"destructing base2 \n";}
};
class derived: public base2,public base1
{ int c;
public:

VAIBHAV PANDEY Page 68


C++ Programming Class XI & XII 2013

derived(int i,int j,int k):base2(i),base1(j)


{
c=k;
cout<<"constructive derived.\n";
};
~derived()
{ cout<<"destructing derived.\n";}
void show()
{ cout<<"1."<<a<<"\t2."<<b<<"\t3."<<c<<"\n";}
};
int main()
{ clrscr();
derived ob(14,15,16);
ob.show();
return 0;
}

OUTPUT :
Constructing Base2
Constructing Base1
Constructing Derived.
1. 15 2. 14 3. 16
destructing Derived.
destructing Base1
destructing Base2
*********************************************************
CONSTRUCTOR AND DESTRUCTOR

Q1. Program to illustrate order of constructor invocation?


Date:11/12/11
Filename:c1.cpp

#include<iostream.h>

VAIBHAV PANDEY Page 69


C++ Programming Class XI & XII 2013

#include<conio.h> //for clrscr()


class subject { int days;
int subjectno;
public:
subject(int d=123,int sn=101);
void printsub(void)
{ cout<<"subject no:"<<subjectno;
cout<<"\n"<<"Days:"<<days<<"\n";
}
} ;
subject::subject(int d,int sn)
{
cout<<"Constructinh subject\n";
days=d;
subjectno=sn;
}
class student { int rollno;
float marks;
public:
student()
{ cout<<"Constructing student\n";
rollno=0;
marks=0.0;
}
void getval(void)
{ cout<<"Enter roll number and marks:";
cin>>rollno>>marks;
cout<<"\n";
}
void print(void)
{ cout<<"Roll no:"<<rollno;
cout<<"\n"<<"Marks:"<<marks<<"\n";
}
};
class admission { subject sub;
student stud;

VAIBHAV PANDEY Page 70


C++ Programming Class XI & XII 2013

float fees;
public:
admission() //constructor
{ cout<<"Constructing admission\n";
fees=0.0;
}
void print(void)
{ stud.print();
sub.printsub();
cout<<"fees:"<<fees<<"\n";
}
};
int main( )
{
clrscr( );
admission adm;
cout<<"\nBack in main( )\n";
return 0;
}

Q2. Program to use an overloaded constructor.


Date:12/12/11
Filename:c2.cpp

#include<iostream.h>
#include<conio.h>
class Deposit
{
long int principal; //PRINCIPAL AMOUNT
int time; //TIME IN YEARS
float rate; //RATE OF INTERAST
float total_amt; //RETURN AMOUNT
public:
Deposit(); //#1
Deposit(long p,int t,float r); //#2

VAIBHAV PANDEY Page 71


C++ Programming Class XI & XII 2013

Deposit(long p,int t); //#3


Deposit(long p,float r); //#4
void calc_amt(void);
};
Deposit::Deposit()
{ pricipal=time=rate=0.0; }
Deposit::Deposit(long p,int t,float r)
{ principal=p;time=t;rate=r;}
Deposit::Deposit(long p,int t)
{ principal=p;time=t;rate=0.08;}
Deposit::drposit(long p,float r)
{ principal=p;time=2;rate=r;}
void Deposit::calc_amt(void)
{
total_amt=principal+(principal*time*rate)/100;
}
void Deposit::display(void)
{
cout<<"\n principal amount:Rs."<<principal;
cout<<"\t period of investment:"<<time<<"years";
cout<<"\n Rate of interst:"<<rate;
cout<<"\n total Amount:Rs."<<total_amt<<"\n";
}
void main()
{
clrscr();
Deposit D1,D2(2000,2,0.07f),D3(4000,1),D4(3000,0.12f);
D1.calc_amt();
D2.calc_amt();
D3.calc_amt();
D4.calc_amt();
cout<<"object 1\n";
D1.display();
cout<<"object 2\n";
D2.display();
cout<<"object 3\n";

VAIBHAV PANDEY Page 72


C++ Programming Class XI & XII 2013

D3.display();
cout<<"object 4\n";
D4.display();
}
OUTPUT:

Object 1:
Principal Amount : Rs.2000
Period of investment : 2 years
Rate of interest : 0.07 Total Amount : Rs.2280

Object 2:
Principal amount : Rs.4000
Period of investment : 1 years
Rate of interest : 0.08 Total Amount : Rs.4320

Object 3:
Principal amount : Rs.3000
Period of investment : 2 years
Rate of interest : 0.12 Total Amount : Rs.3720

Q3. Program to illustrateworking of default arguments. Calculate


interest amount making use of default arguments?
Date : 12/12/11
File name : c3.cpp

#include<iostream.h>
#include<conio.h>
void amount(float princ,int time=2,float rate=0.08); //prototype
void amount(float princ,int time,float rate)
{
cout<<"\n principle amount:Rs."<<princ;
cout<<"\t time:"<<time<<"years";
cout<<"\t Rate:"<<rate;
cout<<"\n Interat amount:"<<(princ * rate * time)<<endl;

VAIBHAV PANDEY Page 73


C++ Programming Class XI & XII 2013

}
void main()
{
clrscr();
cout<<"case 1";
amount(2000);
cout<<"case 2";
amount(2500,3);
cout<<"case 3";
amount(2300,3,0.11);
cout<<"case 4";
amount(2500,0.08);
}

OUTPUT:

Case 1
Principal Amount : Rs.2000 Time : 2 Years
Rate : 0.08 Interest Amount : 320
Case 2
Principal Amount : Rs.2500 Time : 3 Years
Rate : 0.08 Interest Amount : 600
Case 3
Principal Amount : Rs.2300 Time : 3 Years Rate: 0.11
Interest Amount : 759
Case 4
Principal Amount : Rs.2500 Time : 0 Years Rate: 0.08
Interest Amount : 0

Q4. Program to illustrate working of function overloading (as


compared to default arguments). Calculate interest amount
using function overloading?
Date : 12/12/11
File name : c4.cpp

VAIBHAV PANDEY Page 74


C++ Programming Class XI & XII 2013

#include<iostream.h>
#include<conio.h>
void amount(float principal,int time,float rate)//#1
{ cout<<"\nPrincipal amount:Rs."<<principal;
cout<<"\tTime:"<<time<<"years";
cout<<"\tRate:"<<rate;
cout<<"\nInterest amount:"<<(principal*rate*time)<<endl;
}
void amount(float principal,int time)//#2
{ cout<<"\nPrincipal amount:Rs."<<principal;
cout<<"\tTime:"<<time<<"years";
cout<<"\tRate:0.08";
cout<<"\nInterest amount:"<<(principal*0.08f*time)<<endl;
}
void amount(float principal,float rate)//#3
{ cout<<"\nPrincipal amount:Rs."<<principal;
cout<<"\tTime:"<<2<<"years";
cout<<"\tRate:"<<rate;
cout<<"\nInterest amount:"<<(principal*rate*2)<<endl;
}
void amount(int time,float rate)//#4
{ cout<<"\nPrincipal amount:Rs."<<2000;
cout<<"\tTime:"<<time<<"years";
cout<<"\tRate:"<<rate;
cout<<"\nInterest amount:"<<(2000*rate*time)<<endl;
}
void amount(float principal)//#5
{ cout<<"\nPrincipal amount:Rs."<<principal;
cout<<"\tTime:"<<2<<"years";
cout<<"\tRate:"<<0.08;
cout<<"\nInterest amount:"<<(principal*0.08f*2)<<endl;
}
void main()
{ clrscr();
cout<<"Case 1";
amount(2000.0f);

VAIBHAV PANDEY Page 75


C++ Programming Class XI & XII 2013

cout<<"Case 2";
amount(2500.0f,3);
cout<<"Case 3";
amount(2300.0f,3,0.11f);
cout<<"Case 4";
amount(2000.0f,0.12f);
cout<<"Case 5";
amount(6,0.07f);
}

Output
Case 1
Principal Amount : Rs.2000 Time : 2 Years Rate : 0.08
Interest Amount :320
Case 2
Principal Amount : Rs.2500 Time : 3 Years Rate : 0.08
Interest Amount : 600
Case 3
Principal Amount : Rs.2300 Time : 3 Years Rate : 0.11
Interest Amount : 759
Case 4
Principal Amount : Rs.2000 Time : 2 Years Rate : 0.11
Interest Amount : 480
Case 5
Principal Amount : Rs.2000 Time : 6 Years Rate : 0.07
Interest Amount : 840

*********************************************************
ARRAY
Q1. WAP to Linear search in array ?
Date:04/01/12
File name:a1.cpp

#include<iostream.h>

VAIBHAV PANDEY Page 76


C++ Programming Class XI & XII 2013

#include<conio.h>
int lsearch(int[],int,int); //i.e., Lsearch(the array, its size, search_item)
void main()
{
clrscr();
int AR[50],item,n,index; //array can hold max.50 elements
cout<<"Enter desired array size(max.50)...";
cin>>n;
cout<<"\nEnter array elements\n";
for(int i=0;i<n;i++)
{
cin>>AR[i];
}
cout<<"\nEnter element to be searched for...";
cin>>item;
index=lsearch(AR,n,item);
if(index==-1)
cout<<"\nSorry!!Given element could not be found.\n";
else
cout<<"\nElement found at index:"<<index<<",position:"<<index+1<<endl;
}
int lsearch(int AR[],int size,int item) //function to perform linear search
{ for(int i=0;i<size;i++)
{ if(AR[i]==item) return i; //return index of item in case of successful search
return i;
}
return -1;
//the control will reach here only when item is not found
}

INPUT :
Enter desired array size (max. 50)...7
Enter array elements
1 5 2 8 6 10 7
Enter Element to be searched for....6
OUTPUT :

VAIBHAV PANDEY Page 77


C++ Programming Class XI & XII 2013

element found at index : 4, Position : 5

Q2.WAP to find linear search in array?


Date : 04/01/12
File name :a2.cpp

#include<iostream.h>
#include<conio.h>
int bsearch(int[],int,int); //i.e., Bsearch(the array, its size, search_item)
void main()
{
clrscr();
int AR[50],item,n,index; //array can hold max. 50 elements
cout<<"Enter desired array size(max.50)...";
cin>>n;
cout<<"\nEnter array elements(must be sorted in Asc order)\n";
for(int i=0;i<n;i++)
cin>>AR[i];
cout<<"\nEnter element to be searched for...";
cin>>item;
index=bsearch(AR,n,item);
if(index==-1)
cout<<"\nSorry!!Given element could not be found.\n";
else
cout<<"\nElement found at index:"<<index<<",position:"<<index+1<<endl;
}
int bsearch(int AR[],int size,int item) //fuction to perform binary search
{
int beg,last,mid;
beg=0;
last=size-1;
while(beg<=last)
{ mid=(beg+last)/2;
if(item==AR[mid])
return mid;

VAIBHAV PANDEY Page 78


C++ Programming Class XI & XII 2013

else
if(item>AR[mid])
beg=mid+1;
else
last=mid-1;
}
return -1;
//the control will reach here only when item is not found
}

INPUT :
Enter desired array size (max. 50)...7
Enter array element(must be store in ASC order)
2 3 7 8 12 15 1
Enter element ti be stored for.12
OUTPUT:
Element found at index:4 , position:5

Q3. WAP to create insertion in array?


Date : 04/01/12
File name : a3.cpp

#include<iostream.h>
#include<process.h> //for exit()
#include<conio.h>
int findpos(int[],int,int); //fuction to determine the right postion for new element
void main()
{
clrscr();
int AR[50],item,n,index; //array can be hold max. 50 elements
cout<<"How many element do U want to create array with?(max.50)...";
cin>>n;
cout<<"\nEnter array element(must be sorted in asc order)\n";
for(int i=0;i<n;i++)
cin>>AR[i];

VAIBHAV PANDEY Page 79


C++ Programming Class XI & XII 2013

char ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter element to be inserted...";
cin>>item;
if(n==50)
{ cout<<"overflow!!\n";
exit(1);
}
index=findpos(AR,n,item);
for(i=n;i>index;i--)
{ AR[i]=AR[i-1]; } //shift elements to create room for new element
AR[index]=item; //item inserted
n += 1; //Number of elements updated
cout<<"\nWant to insert more elements? (y/n)...";
cin>>ch;
}
cout<<"The array now is as shown below...\n";
for(i=0;i<n;i++)
cout<<AR[i]<<" ";
cout<<endl;
}
int findpos(int AR[],int size,int item) //function to determine the position for new element
{
int pos;
if(item<AR[0])
pos=0;
else
{ for(int i=0;i<size-1;i++)
{ if(AR[i]<=item&&item<AR[i+1])
{
pos = i+1;
break;
}
}
if(i==size-1)
pos=size;

VAIBHAV PANDEY Page 80


C++ Programming Class XI & XII 2013

}
return pos;
}

INPUT :

How many elelments do u want to create array with? (max. 50)...6


enter Array elements (must be stored in Asc order)
2 6 9 10 12 15
Enter Elements to be inserted ....11
Want to insert more elements ? (y/n)...y

Enter Elements to be inserted ....4


Want to insert more elements ? (y/n)...n

OUTPUT :
The arrray now is as shown below...
2 4 6 9 10 11 12 15

Q4. WAP create deletion in array?


Date:04|01|12
File name : a4.cpp

#include<iostream.h>
#include<process.h> //for exit()
#include<conio.h>
int lsearch(int[],int,int); //function to search for given element
void main()
{
clrscr();
int AR[50],item,n,index; //array can hold max. 50 elements
cout<<"How many element do U want to create array with?(max.50)...";
cin>>n;
cout<<"\nEnter array element...\n";
for(int i=0;i<n;i++)

VAIBHAV PANDEY Page 81


C++ Programming Class XI & XII 2013

cin>>AR[i];
char ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter element to be deleted...";
cin>>item;
if(n==0)
{ cout<<"Underflow!!\n";
exit(1);
}
index=lsearch(AR,n,item);
if(index!=-1)
AR[index]=0;
else
cout<<"Sorry!! No such element in the array.\n";
cout<<"\nThe array now is as shown below...\n";
cout<<"Zer0 (0) signifies deleted element\n";
for(i=0;i<n;i++)
cout<<AR[i]<<" ";
cout<<endl;
cout<<"after this emptied space will be shifted to the end of array\n";
for(i=index;i<n;i++)
{ AR[i]=AR[i+1]; } //shift elements to bring empty space at the end of array
n -= 1; //Number of elements of updated
cout<<"\nWant to insert more elements? (y/n)...";
cin>>ch;
}
cout<<"The array after shifting ALL emptied spaces towards right is...\n";
for(i=0;i<n;i++)
cout<<AR[i]<<" ";
cout<<endl;
}
int lsearch(int AR[],int size,int item) //fuction to perform linear search
{
for(int i=0;i<size-1;i++)
{ if(AR[i]==item)
return i;

VAIBHAV PANDEY Page 82


C++ Programming Class XI & XII 2013

return -1;
//the control will reach here only when itemis not found
}

INPUT :
How many elements do u want to create array with? (max. 50)...10
Enter Array elements....
2 4 5 3 7 9 12 15 33 40

Enter Elements to be deleted ...9


The array now is as shown below...
Zero (0) signifies deleted element
2 4 5 3 7 0 12 15 33 40
After this emptied space will be shifted to the end of array

want to delete more elements? (y/n)...n


OUTPUT :
The array after shifting ALL emptied spaces towards right is...
2 4 5 3 7 12 15 33 40

Q5 WAP to create selsort in an array?


DATE :4|01|12
FILE NAME:a5.cpp

#include<iostream.h>
#include<conio.h>
void SelSort(int[ ],int); //function for selection sort
void main()
{ int AR[50],item,n,index; //array can hold max. 50 elements
cout<<"How many element do U want to create array with?(max.50)...";
cin>>n;
cout<<"\nEnter array element...\n";

VAIBHAV PANDEY Page 83


C++ Programming Class XI & XII 2013

for(int i=0;i<n;i++)
cin>>AR[i];
selsort(AR,n);
cout<<"\n\nThe sorted array is as shown below...\n";
for(i=0;i<n;i++)
cout<<AR[i]<<" ";
cout<<endl;
}
void selsort(int AR[],int size) //function to perform Slection Sort
{ int pos,small,temp;
for(int i=0;i<size;i++)
{ small=AR[i];
for(int j=i+1;j<size;j++)
{ if(AR[j]<small)
{
small=AR[j];
pos=j;
}
}
temp=AR[i];
AR[i]=AR[pos];
AR[pos]=temp;
cout<<"\nArray after pass ="<<i+1<<"- is:";
for(j=0;j<size;j++)
cout<<AR[j]<<" ";
}
}

INPUT :
How many elements do u want to create array with? (max.50)...6
Enter Array elements...6 8 2 5 3 4
Array after pass - 1- is : 2 8 5 6 3 4
Array after pass - 2- is : 2 3 5 6 8 4
Array after pass - 3- is : 2 3 4 6 8 5
Array after pass - 4- is : 2 3 4 6 8 6

VAIBHAV PANDEY Page 84


C++ Programming Class XI & XII 2013

Array after pass - 5- is : 2 3 4 6 6 8


Array after pass - 6- is : 2 3 4 6 6 8
OUTPUT :
The sorted array is as shown below...
2 3 4 5 6 8

Q6. WAP to create a bubble sort in array?


Date : 04/12/11
File name : a6.cpp

#include<iostream.h>
#include<conio.h>
void bubblesort(int[],int) ; //fuction for bubble sort
void main()
{ clrscr();
int AR[50],item,n,index; //array can hold max. 50 elements
cout<<"How many element do U want to create array with?(max.50)...";
cin>>n;
cout<<"\nEnter array element...\n";
for(int i=0;i<n;i++)
cin>>AR[i];
bubblesort(AR,n);
cout<<"\n\nThe sorted array is as shown below...\n";
for(i=0;i<n;i++)
cout<<AR[i]<<" ";
cout<<endl;
}
void bubblesort(int AR[],int size) //function to perform Bubble Sort
{ int temp,ctr=0;
for(int i=0;i<size;i++)
{ for(int j=i+1;j<size;j++)
{ if(AR[j]<AR[j+1])
{
temp=AR[j];
AR[j]=AR[j+1];
AR[j+1]=temp;

VAIBHAV PANDEY Page 85


C++ Programming Class XI & XII 2013

}
}
cout<<"array after iteration - "<<++ctr<<"- is: ";
for(int k=0;k<size;k++)
cout<<AR[k]<<" ";
cout<<endl;
}
}

INPUT :
How many elements do u want to create array with? (max. 50)...5

Enter Array elements....


9 7 4 6 1
Array after iteration - 1 - is : 74619
Array after iteration - 2 - is : 46179
Array after iteration - 3 - is : 41679
Array after iteration - 4 - is : 14679
Array after iteration - 5 - is : 14679

OUTPUT :
The Sorted array is as shown below....
4 6 7 9

Q7. WAP to create insertion sort in an array?


Date : 06/01/12
File name : a7.cpp

#include<iostream.h>
#include<conio.h>
#include<limits.h> //for INT_MIN
void inssort(int[],int) ; //fuction for insertion sort
void main()
{ clrscr();
int AR[50],item,n,index; //array can hold max.50 elements

VAIBHAV PANDEY Page 86


C++ Programming Class XI & XII 2013

cout<<"How many element do U want to create array with?(max.50)...";


cin>>n;
cout<<"\nEnter array element...\n";
for(int i=0;i<n;i++)
cin>>AR[i];
inssort(AR,n);
cout<<"\n\nThe sorted array is as shown below...\n";
for(i=1;i<=n;i++)
cout<<AR[i]<<" ";
cout<<endl;
}
void inssort(int AR[],int size) //function to perform Bubble Sort
{ int tmp,j;
AR[0]=INT_MIN;
for(int i=1;i<=size;i++)
{
tmp=AR[i];
j=i-1;
while(tmp<AR[j])
{
AR[j+1]=AR[j];
j--;
}
AR[j+1]=tmp;
cout<<"array after pass - "<<i<<"- is: ";
for(int k=1;k<=size;k++)
cout<<AR[k]<<" ";
cout<<endl;
}
}
INPUT :
How many elements do u want to create array with ? (max. 50)....7
Enter Array elements....9 4 3 2 34 1 6
Array after pass - 1 - is : 9 4 3 2 34 1 6
Array after pass - 2 - is : 4 9 3 2 34 1 6
Array after pass - 3 - is : 3 4 9 2 34 1 6

VAIBHAV PANDEY Page 87


C++ Programming Class XI & XII 2013

Array after pass - 4 - is : 2 3 4 9 34 1 6


Array after pass - 5 - is : 2 3 4 9 34 1 6
Array after pass - 6 - is : 1 2 3 4 9 34 6
Array after pass - 7 - is : 1 2 3 4 6 9 34

OUTPUT :
The sorted array is as shown below....
1 2 3 4 6 9 34

Q8. WAP to find Sum of matrices?


Date : 06/01/12
File name : a8.cpp

#include<iostream.h> //For exit() fuction


#include<process.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],c[10][10];
int i,j,m,n,p,q;
cout<<"\nInput row and column of matrix-A \n";
cin>>m>>n;
cout<<"\nInput row and column of matrix-B \n";
cin>>p>>q;
if((m==p)&&(n==q)) //Check if matrix can be added
cout<<"Matrices can be added\n";
else
{
cout<<"\nMatrices cannot be added\n";
exit(0);
}
cout<<"\nInput matrix-A\n";
for(i=0;i<m;i++)
{

VAIBHAV PANDEY Page 88


C++ Programming Class XI & XII 2013

for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"\nInput matrix-B\n"; //Loop to accept matrix B.
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
cin>>b[i][j];
}
for(i=0;i<m;i++) //Addition of two matrices.
{
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
}
cout<<"\nThe sum of two matrices is:\n";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<" "<<c[i][j];
}
}

INPUT :
Input row and column of matrix - A
22

Input row & column of matrox - B


22
Matrices can be added

Input matrix - A
12
34

VAIBHAV PANDEY Page 89


C++ Programming Class XI & XII 2013

Input matrix - B
43
21

OUTPUT :
The sum of two matrices is :
55
55

Q9. WAP to find Difference of matrices?


Date : 06|01|12
File name : a9.cpp

#include<iostream.h>
#include<process.h> //For exit() function
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],c[10][10];
int i,j,m,n,p,q;
cout<<"\nInput row and column of matrix-A \n";
cin>>m>>n;
cout<<"\nInput row and column of matrix-B \n";
cin>>p>>q;
if((m==p)&&(n==q)) //Check if matrix can be subtracted.
cout<<"Matrices can be subtracted\n";
else
{
cout<<"\nMatrices cannot be subtracted\n";
exit(0);
}
cout<<"\nInput matrix-A\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)

VAIBHAV PANDEY Page 90


C++ Programming Class XI & XII 2013

cin>>a[i][j];
}
cout<<"\nInput matrix-B\n"; //Loop to accept matrix B.
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
cin>>b[i][j];
}
for(i=0;i<m;i++) //Subtraction of two matrices.
{
for(j=0;j<n;j++)
c[i][j]=a[i][j]-b[i][j];
}
cout<<"\nThe difference of two matrices is:\n";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<" "<<c[i][j];
}
}

INPUT :
Input row & column of matrix-A
23
Input row & column of matrix-B
23
Matrices can be subtracted
Input matrix-A
648
274
Input matrix-B
123
001

OUTPUT :

VAIBHAV PANDEY Page 91


C++ Programming Class XI & XII 2013

The difference of two matrices is :


525
73

Q10. WAP to find Product of Matrices?


Date : 06/01/12
File name : a10.cpp

#include<iostream.h>
#include<process.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],c[10][10];
int i,j,m,n,p,q,ip;
cout<<"\nInput row and column of matrix-A \n";
cin>>m>>n;
cout<<"\nInput row and column of matrix-B \n";
cin>>p>>q;
if(n==p) //check if matrices can be multiplied or not
cout<<"Matrices can be multiplied\n";
else
{
cout<<"\nMatrices cannot be multiplied\n";
exit(0);
}
cout<<"\nInput matrix-A\n";
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
cin>>a[i][j];
}
cout<<"\nInput matrix-B\n";
for(i=0;i<p;++i)
{

VAIBHAV PANDEY Page 92


C++ Programming Class XI & XII 2013

for(j=0;j<q;++j)
cin>>b[i][j];
}
for(i=0;i<m;++i) //Multiply two matrices
for(j=0;j<q;++j)
{ c[i][j]=0;
for(ip=0;ip<n;++ip)
c[i][j]=a[i][ip]*b[ip][j];
}
cout<<"\nThe product of A & B matrices is:\n";
for(i=0;i<m;++i)
{
cout<<"\n";
for(j=0;j<q;++j)
cout<<c[i][j]<<" ";
}
}

INPUT :
Input row & column of A matrix :
23
Input row & column of B matrix :
32
Matrices can be multiplied.

Input matrix-A
123
123

Input matrix-B
12
12
12

OUTPUT :
Product of A & B matrices :

VAIBHAV PANDEY Page 93


C++ Programming Class XI & XII 2013

6 12
6 12
*********************************************************
DATAFILE HANDLING
Q1. Get rollnumbers and marks of the students of a
class(getfromtheuser)and store these details
into a file called Marks.dat.
Date : 04/02/12
File name : d1.cpp

#include<fstream.h>
void main()
{
ofstream filout;
//stream decided and declared - steps 1&2
filout.open("marks.dat",ios::out);
//file linked - step 3
char ans='y';
//process as required - steps 4 begins
int rollno;
float marks;
while(ans=='y'||ans=='Y')
{
cout<<"enter rollno";
cin>>rollno;
cout<<"enter marks";
cin>>marks;
filout<<rollno<<'\n'<<marks<<'\n';
cout<<"\n want to enter more records?(y/n)";
cin>>ans;
}
filout.close(); //delink the file - step 5
}

VAIBHAV PANDEY Page 94


C++ Programming Class XI & XII 2013

Q2. Program to create a single file and then displayits contents.


Date : 04/12/11
File name :d2.cpp

#include<fstream.h>
#include<conio.h> //for clrscr()
int main()
{
clrscr();
ofstream fout("student");
//connect student file to output stream fout
char name[30],ch;
float marks=0.0;
for(int i=0;i<5;i++)
{
cout<<"student"<<(i+1)<<":\tName:";
cin.get(name,30);
cout<<"\t\tmarks:";
cin>>marks;
cin.get(ch); //to empty input buffer
fout<<name<<'\n'<<marks<<'\n';
}
fout.close(); //disconnect student file from fout
ifstream fin("student"); //connect student file to input stream fin
fin.seekg(0); //to bring file pointer at the file beginning
cout<<"\n";
for(i=0;i<5;i++) //Display records
{
fin.get(name,30); //read name from file student
fin.get(ch);
fin>>marks; //read marks from file student
fin.get(ch);
cout<<"student name:"<<name;
cout<<"\tmarks:"<<marks<<"\n";
}
fin.close(); //disconnect sudent file from fin stream

VAIBHAV PANDEY Page 95


C++ Programming Class XI & XII 2013

return 0;
}
INPUT:
Student 1 : Name : Raju Shah
Marks : 88
Student 2 : Name : Neil Sarkar
Marks : 90
Student 3 : Name : Nick Robert
Marks : 78
Student 4 : Name : M.Ramesh
Marks : 67
Student 5 : Name : S.Anis
Marks : 73
OUTPUT:
Student Name : Raju Shah Marks : 88
Student Name : Neil Sarkar Marks : 90
Student Name : Nick Robert Marks : 78
Student Name : M.Ramesh Marks : 67
Student Name : S.Anis Marks : 73

Q3. Program to use multiple files in succession.


Date : 04/02/12
File name : d3.cpp

#include<fstream.h>
#include<conio.h> //for clrscr()
int main()
{
clrscr();
ofstream filout; //create output stream
filout.open("stunames");
filout<<"devyani\n"<<"monica\n"<<"neil banergee\n"; //write to stunames file
filout.close(); //disconnect stunames and
filout.open("stumarks"); //connect stumarks
filout<<"78.09\n"<<"78.90\n"<<"89.23\n"; //write to stumarks file
filout.close(); //disconnect stumarks

VAIBHAV PANDEY Page 96


C++ Programming Class XI & XII 2013

char line[80];
ifstream filin; //create input stream
filin.open("stunames"); //connect stunames to it
cout<<"the content of stunames files are given below";
filin.getline(line,80); //read a line
cout<<line<<"\n"; //display it
filin.getline(line,80);
cout<<line<<"\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.close(); //disconnect stunames and
filin.open("stumarks"); //connect stumarks
cout<<"\n the content of stumarks are given below";
filin.getline(line,80); //read a line
cout<<line<<"\n"; //display it
filin.getline(line,80);
cout<<line<<"\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.close();
return 0;
}

Input
The content of stunames file are given below
Devyani
Monika patrick
Neil banerjee

The content of stumarks file are given below


78.09
78.90
89.23

Q4. Write a program to multiple files simultaneously?

VAIBHAV PANDEY Page 97


C++ Programming Class XI & XII 2013

Date : 04/02/12
File name : d4.cpp

#include<fstream.h>
#include<conio.h> //for clrscr()
int main()
{ clrscr();
//Assuming that the two files(stunames & stumarks)
are already created
ifstream filin1,filin2; //create two input
Stream filin1.open("stunames");
//connect stunames to
First stream
filin2.open("stumarks"); //connect stumarks to second stream
char line[80];
cout<<"The contents of stunames and stumarks are given below.\n";
filin1.getline(line,80);
cout<<line<<"\n";
filin2.getline(line,80);
cout<<line<<"\n";
filin1.getline(line,80);
cout<<line<<"\n";
filin2.getline(line,80);
cout<<line<<"\n";
filin1.getline(line,80);
cout<<line<<"\n";
filin2.getline(line,80);
cout<<line<<"\n";
filin1.close();
filin2.close();
return 0;
}

Input
The contents of stunames and stumarks are given below.
Priyam

VAIBHAV PANDEY Page 98


C++ Programming Class XI & XII 2013

78.92
Monika patrick
72.17
Neil banerjee
69.33
Q5. WAP that reads more such details and appends them to this
file. Make sure that previous contents of the file are not lost?

Date : 04/02/12
File name : d5.cpp

#include<fstream.h>
#include<conio.h>
void main()
{
ofstream fout; //stream decided and declared - step 1&2
fout.open("marks.dat",ios::app); //file linked - step 3
char ans='y'; //process as required - step 4 begins
int rollno;
float marks;
clrscr();
while(ans=='y'||ans=='y')
{
cout<<"\n enter rollno.:";
cin>>rollno;
cout<<"\n enter marks:";
cin>>marks;
fout<<rollno<<'\n'<<marks<<'\n';
cout<<"\n want to enter more record?(y/n)...";
cin>>ans;
}
fout.close(); //delink the file - step 5
}

VAIBHAV PANDEY Page 99


C++ Programming Class XI & XII 2013

INPUT:
Enter Rollno. : 101
Enter Marks : 78
Want to enter more records?(y/n)....y

Enter Rollno. : 102


Enter Marks : 86
Want to enter more records?(y/n)....y

Enter Rollno. : 103


Enter Marks : 79
Want to enter more records?(y/n)....n

Q6. Program for reading and writing class objects?


Date : 04/06/12
File name : d6.cpp

#include<fstream.h>
#include<conio.h> //for clrscr()
class student { char name[40];
char grade;
float marks;
public:
void getdata(void);
void display(void);
};
void student::getdata(void)
{ char ch;
cin.get(ch);
cout<<"Enetr name:";
cin.getline(name,40);
cout<<"Enter grade:";
cin>>grade;
cout<<"Enter marks:";

VAIBHAV PANDEY Page 100


C++ Programming Class XI & XII 2013

cin>>marks;
cout<<"\n";
}
void student::display(void)
{ cout<<"name:"<<name<<"\t"
<<"grade:"<<grade<<"\t"
<<"marks:"<<marks<<"\t"<<"\n";
}
void main()
{ clrscr();
student arts[3]; //declare array of 3 objects
fstream filin; //input and output file
filin.open("stu.dat",ios::in|ios::out);
if(!filin)
{ cout<<"Cannot open file!!\n";
return 1;
}
cout<<"Enter details for 3 students \n";
for(int i=0;i<3;i++)
{ arts[i].getdata();
filin.write((char*)&arts[i],sizeof(arts[i]));
}
filin.seekg(0); //seekg(0) resets the file to start ,so that the file
//can be accessed from the beginning.
cout<<"The contents of stu.dat are shown below.\n";
for(i=0;i<3;i++)
{ filin.read((char*)&arts[i],sizeof(arts[i]));
arts[i].display();
}
filin.close();
}

INPUT :
Enter details for 3 students

Enter name :Darpan aggarwal

VAIBHAV PANDEY Page 101


C++ Programming Class XI & XII 2013

Enter garde :B
Enter marks :66

Enter name :Surendra Goyal


Enter garde :A
Enter marks :75

Enter name :Sumit Kumar


Enter garde :B
Enter marks :67
OUTPUT:
The contents of stu.dat are shown below.
Name :Darpan aggarwal Grade :B Marks :66
Name :Surendra goyal Grade :A Marks :75
Name :Sumit kumar Grade :B Marks :67

Q7. Program to write and read a structure using write() and read()
fuction using a binary file?
Date : 04/02/12
File name : d7.cpp

#include<fstream.h>
#include<string.h>
#include<conio.h> //for clrscr()
struct customer
{char name[51];
float balance;
};
void main()
{
clrscr();
customer save;
strcpy(savac.name,"tina Marshall"); //copy value to sructure
savac.balance=21310.75; //variable savac

VAIBHAV PANDEY Page 102


C++ Programming Class XI & XII 2013

ofstream fout;
fout.open("savaing,ios::out|ios::biniary); //open output file
if(!fout);
{
cout<<"file cant\t be opened\n";
return 1;
}
fout.write((char*)&savac,size of(customer)); //write to file
fout.close(); //close connection
//read it back now
ifstream fin;
fin.open("savaing",ios::in|ios::binary); //open input file
fin.read((char*)&savac,size of(customer)); //read structure
cout<<savac.name; //display structure now
cout<<"has the balance amount of Rs."<<savac.balance<<"\n";
fin.close();
}

OUTPUT :
Tina Marshall has the balance amount of Rs. 21310.75

*********************************************************
POINTERS
Q.1 Program to create two array to store roll number and marks of
some student would be known at run.(to illustrate the working
of free store operation).
DATE :4|08|12
FILENAME : P1.CPP

#include<iostream.h>
#include<conio.h>
int *rollno;
float *marks;
int main()

VAIBHAV PANDEY Page 103


C++ Programming Class XI & XII 2013

{
int size;
cout<<"how many element are there in array";
cin>>size;
rollno=new int[size];
marks=new float[size];
if((!rollno)||(!marks))
{ cout<<"out of memory!aborting!";
return 1;
}
for(int i=0;i<size;i++)
{
cout<<"enter roll no and for student"<<(i+1);
cin>>rollno[i]>>marks[i];
}
cout<<"\troll no\tmarks\n";
for(i=0;i<size;i++)
cout<<"\t"<<rollno[i]<<"\t\t"<<marks[i];
delete[]rollno;
delete[]marks;
return 0;
}

INPUT:
how many elements are there in the array ?
3
enter rollno and marks for student 1
1 78
enter rollno and marks for student 2
2 54
enter rollno and marks for student 3
3 91
OUTPUT:
rollno marks
1 78
2 54

VAIBHAV PANDEY Page 104


C++ Programming Class XI & XII 2013

3 91

Q.2 Program to read a 2-D array using pointers (i.e. , Dynamic


array),calculate its rowsum and column-sum and display this
array alongwith rowsum and column -sum.
Date:4|08|12
Filename:p2.cpp

#include<iostream.h>
void main()
{
int *val,*Rsum,*Csum;
int maxR,maxC,i,j;
cout<<"Enter dimensions (row col):";
cin>>maxR>>maxC;
val=new int[maxR*maxC]; //actual 2-Darray
Rsum=new int[maxR]; //array to hold row sums
Csum=new int[maxC];
for(i=0;i<maxR;i++)
{ cout<<"\nEnter elements of row"<<i+1<<":";
Rsum[i]=0;
for(j=0;j<maxC;j++)
{ cin>>val[i*maxC+j];
Rsum[i] += val[i*maxC+j];
}
}
for(j=0;j<maxC;j++)
{ Csum[j]=0;
for(i=0;i<maxR;i++)
{
Csum[j] += val[i*maxC+j];
}
}
cout<<"\n\nThe given array along with Rowsum and Colsum is:\n\n";
for(i=0;i<maxR;i++)

VAIBHAV PANDEY Page 105


C++ Programming Class XI & XII 2013

{ for(j=0;j<maxC;j++)
{
cout<<val[i*maxC+j]<<'\t';
}
cout<<Rsum[i]<<endl;
}
for(j=0;j<maxC;j++)
{
cout<<Csum[j]<<'\t';
}
cout<<endl;
}
INPUT:
Enter dimensions(row col): 3 3
Enter elements of row 1: 1 2 3
Enter elements of row 2: 2 1 1
Enter elements of row 3: 3 2 2
OUTPUT:
The given array along with Rowsum and Colsum is:
1 2 3 6
2 1 1 4
3 2 2 7
6 5 6

Q.3 WAP to print different values being pointing to by an array of


pointers?
Date:4|08|12
Filename:p3.cpp

#include<iostream.h>
void main()
{
int *ip[5]; //array of 5 int pointers
int f=65,s=67,t=69,fo=70,fi=75 //5 int variables
//initialize array pointer s by making them points to 5 different ints

VAIBHAV PANDEY Page 106


C++ Programming Class XI & XII 2013

ip[0]=&f; ip[1]=&s; ip[2]=&t;


ip[3]=&fo; ip[4]=&fi;
//print the values being pointed to by the pointers
for(int i=0;i<5;i++);
cout<<"the pointer ip["<<i<<"]points tp"<<*ip[i]<<"\n";
//print the addresses stored in the array
cout<<"the base address of array ip of pointer is"<<ip<<"\n";
for(i=0;i<5;i++)
cout<<"the address stored in ip["<<i<<"] is"<<ip[i]<<"\n";
}

OUTPUT:
the pointer ip[0] points to 65
the pointer ip[1] points to 67
the pointer ip[2] points to 69
the pointer ip[3] points to 70
the pointer ip[4] points to 75
the base address of array ip of pointers is 0x22f72422
the address stored in ip[0] is 0x22f72458
the address stored in ip[1] is 0x22f72456
the address stored in ip[2] is 0x22f72454
the address stored in ip[3] is 0x22f72452
the address stored in ip[4] is 0x22f72450

Q.4 Program to change the position of string in arrayusing array of


pointer.
DATE :8|08|12
FILENAME : p4.CPP

#include<iostream.h>
#include<string.h>
void main()
{
char*names[]={"sachin","kaoil","ajay","sunil","anil};
int len=0;

VAIBHAV PANDEY Page 107


C++ Programming Class XI & XII 2013

len=strlen(names[1]);
cout<<"\n Originally string 2 is";
cout<<"and string 4 is";
char *t;
t=names[1];
names[1]=names[3];
names[3]=t;
len=strlen(names[1]);
cout<<"exchange string 2 is";
cout.write(names[1],len).put('\n');
cout<<"and string 4 is";
cout.write(names[3],len).put('\n');
}

OUTPUT:
originally string 2 is kapil
and string 4 is sunil
exchanged string 2 is sunil
and string 4 is kapil

Q.5 Program to swap value of two variables using pass by


reference method.
DATE :8|08|12
FILENME : p5.CPP

#include<iostream.h>
#include<conio.h>
void main()
{
void swap(int &,int &);
int a=7,b=5;
cout<<"original values\n";
cout<<"a="<<a<<"b,="<<b<<"\n";
swap(a,b);
cout<<"swapped values\n";

VAIBHAV PANDEY Page 108


C++ Programming Class XI & XII 2013

cout<<"a="<<a<<",b="<<b<<"\n";
}
void swap(int & x,int & y)
{
int temp;
temp=x;
x=y;
y=temp;
}

OUTPUT:
original value
a=7,b=4
swapped value
a=4,b=7

Q.6 Program to sawp values of variable by passing pointers.


DATE:9|08|12
FILENAME:p6.cpp

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
void swap(int*x,int*y);
int a=7,b=4;
cout<<"Original Values\n";
cout<<"a="<<a<<",b="<<b<<"\n";
swap(&a,&b);
cout<<"swapped values\n";
cout<<"a="<<a<<",b="<<b<<"\n";
}
void swap(int *x,int*y)
{

VAIBHAV PANDEY Page 109


C++ Programming Class XI & XII 2013

int temp;
temp=*x;
*x=*y;
*y=temp;
}
OUTPUT:
Original values
a=7 , b=4
swapped values
a=4 , b=7
***************************************************
LINKED LISTS , STACKS AND QUEUES

Q.1 Write a program to create and traverse a linked list.The linked


list contains data of integer type.
Date:4|10|12
Filename:lsq1.cpp

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *link;
};
void main()
{
node *first,*temp,*last;
int n,i;
clrscr();
cout<<"\nEnter how many nodes to be create in the linked list";
cin>>n;

VAIBHAV PANDEY Page 110


C++ Programming Class XI & XII 2013

first=new node;
cout<<"\nEnter the data value of node ->";
cin>>first->data;
first->link=NULL;
temp=first;
for(i=0;i<n;i++)
{
last= new node;
cout<<"\nEnter the data value of node "<<i+1<<"->";
cin>>last-> data;
last->link=NULL;
temp->link=last;
temp=last;
}
temp=first;
clrscr();
cout<<"The linked list value are:\n";
while(temp!=NULL)
{
cout<<"\n"<<temp->data;
temp=temp->link;
}
getch();
}

INPUT:
Enter how many nodes to create in the linked list: 2
Enter the data value of node-> 1
Enter the data value of node1-> 2
Enter the data value of node2-> 3

OUTPUT:
The linked list values are :

VAIBHAV PANDEY Page 111


C++ Programming Class XI & XII 2013

1
2
3

Q.2 Write a program to create a linked and searched a particular


data value of integer data type
Date:4|10|12
Filename:lsq2.cpp
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *link;
};
void main()
{
node *first,*temp,*last;
int n,i,flag,item;
clrscr();
cout<<"\n\tEnter how many nodes to be create in the linked list->";
cin>>n;
first=new node;
cout<<"\n\t Enter the data value of node 1";
cin>>first->data;
first->link=NULL;
temp=first;
for(i=1;i<n;i++)
{
last= new node;
cout<<"\n\t\ Enter the data value of node->"<<i+1
<<; cin>>last-> data;

VAIBHAV PANDEY Page 112


C++ Programming Class XI & XII 2013

last->link=NULL;
temp->link=last;
temp=last;
}
temp=first;
clrscr();
cout<<"The linked list value are:\n";
while(temp!=NULL)
{
cout<<"\n"<<temp->data;
temp=temp->link;
}
flag=0;
cout<<"\n\ndata value to be searched-->";
cin>>item;
temp=first;
while(temp!=NULL)
{
if(temp->data==item)
{
flag=1;
break;
}
temp=temp->link;
}
if(flag==1)
cout<<"\n\n\tsearch is successful";
else
cout<<"\n\n\tsearch is unsuccessful";
getch();
}

INPUT:
Enter how many nodes to create in the linked list: 2
Enter the data value of node1-> 2
Enter the data value of node2-> 7

VAIBHAV PANDEY Page 113


C++ Programming Class XI & XII 2013

OUTPUT:
The linked list value are:
2
7
data value to be searched->2
search is suceesfull

Q.3 Write a program to create a linked list and insert elements


according to users choice?
Date:4|10|12
Filename:lsq3.cpp

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *link;
};
node *addfirst(node*first,int value);
node *addbetween(node*first,int value,int val);
node *addlast(node*first,int value);
void traverse(node *first);
void main()
{
clrscr();
int i,n, choice,val,value;
node *first,*temp,*last;
cout<<"enter how many nodes to created in link list->";
cin>>n;
first=new node;
cout<<"\nenter the element of node 1->";

VAIBHAV PANDEY Page 114


C++ Programming Class XI & XII 2013

cin>>first->data;
first->link=NULL;
temp=first;
for(i=1;i<n;i++)
{
last=new node;
cout<<"\nenter the element of node "<<i+1<<"->";
cin>>last->data;
last->link=NULL;
temp->link=last;
temp=last;
}
do
{
cout<<"\nmain menu";
cout<<"\n1.inserting at first";
cout<<"\n2.inserting at between";
cout<<"\n3.insert at last";
cout<<"\n4.transversing at last";
cout<<"\n5.exit";
cout<<"\nenter your choice->";
cin>>choice;
switch(choice)
{
case 1:cout<<"\nenter data value to inserted->";
cin>>value;
first=addfirst(first,value);
break;
case 2:cout<<"enter data value to be inserted-->";
cin>>value;
cout<<"enter the value of node after which insertion is made-->";
cin>>val;
first=addbetween(first,value,val);
break;
case 3:cout<<"enter the data to be inserted-->";
cin>>value;

VAIBHAV PANDEY Page 115


C++ Programming Class XI & XII 2013

first=addlast(first,value);
break;
case 4:traverse(first);
break;
case 5:exit(0);
}
}
while(choice!=0);
}
void traverse(node*first)
{
node*temp;
temp=first;
cout<<"the linked list value are-->";
while (temp!=NULL)
{
cout<<"\n"<<temp->data;
temp=temp->link;
}
}
node*addfirst(node*first,int value)
{
node*temp;
temp=new node;
temp->data=value;
temp->link=first;
first=temp;
return(first);
}
node*addbetween(node*first,int value,int val)
{
node *temp;
node *temp1;
temp=new node;
temp->data=value;
temp1=first;

VAIBHAV PANDEY Page 116


C++ Programming Class XI & XII 2013

while(temp1!=NULL)
{
if(temp1->data==val)
{
temp->link=temp1->link;
break;
}
temp1=temp1->link;
}
return(first);
}
node*addlast(node*first,int value)
{
node *temp;
node *temp1,*back;
temp=new node;
temp->data=value;
temp->link=NULL;
back=first;
temp1=first->link;
while(temp1!=NULL)
{
back=temp1;
temp1=temp1->link;
}
back->link=temp;
return(first);
}

OUTPUT:
enter how many to be created in linked list -> 2
enter element of node 1-> 1
enter element of node 2 ->2
main menu:
1. Inserting at first
2. Inserting in between

VAIBHAV PANDEY Page 117


C++ Programming Class XI & XII 2013

3. Inserting at last
4. traversing at last
5. exit
enter your choice-> 5

Q.4 Write a program to create a linked list and delete elements


according to users choice?
Date:4|10|12
Filename:lsq4.cpp

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *link;
};
//function for deleting element in linked list.
node *delfirst(node *first,int &value);
node *delbetween(node *first,int &value,int val);
node *dellast(node *first,int &value);
void traverse(node *first);
//main programming logic
void main()
{
clrscr();
node *first, *temp, *last;
int i,n,value,choice,val;
cout<<"\n\n Enter how many nodes in the linked list:";
cin>>n;
//creation of linked list
first=new node;
cout<<"\n\n Enter the data value of node 1:";

VAIBHAV PANDEY Page 118


C++ Programming Class XI & XII 2013

cin>>first->data;
first->link=NULL;
temp=first;
for(i=1;i<n;i++)
{
last=new node;
cout<<"\n\n Enter the data value of node "<<i+1<<":";
cin>>last->data;
last->link=NULL;
temp->link=last;
temp=last;
temp=last;
}
do
{
cout<<"\n\t main menu:";
cout<<"\n\t 1. For deleting at first";
cout<<"\n\t 2.For deleting in between";
cout<<"\n\t 3.For deleting at last";
cout<<"\n\t 4.Traversing the list";
cout<<"\n\t 5.For exit";
cout<<"\n\t Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:first=delfirst(first,value);
if(value!=-1)
cout<<"\n\t The deleted data is:"<<value<<"\n\t";
break;

case 2:cout<<"\n\t Enter the value of node which is to be deleted:";


cin>>val;
first=delbetween(first,value,val);
if(value!=-1)
cout<<"\n\t The deleted value is:"<<value<<\n\t
break;

VAIBHAV PANDEY Page 119


C++ Programming Class XI & XII 2013

case 3:first=dellast(first,value);
if(value!=-1)
cout<<"\n\tThe deleted value is:" << value<< \n\t;
break;

case 4:traverse(first);
break;

case 5:exit(0);
}
}while(choice!=5);
}void traverse(node*first)
{
node *temp;
temp=first;
cout<<"\n the linked listed values are";
while(temp!=NULL)
{
cout<<"\n"<<temp->data;
temp=temp->link;
}
}

node*delfirst(node*first,int&value)
{
node *temp;
temp=first;
if(first==NULL)
{
cout<<"\n list empty";
value=-1;
}
else
{
value=temp->data;

VAIBHAV PANDEY Page 120


C++ Programming Class XI & XII 2013

first=first->link;
temp->link=NULL;
delete(temp);
}
return(first);
}
node*delbetween(node*first,int &value,int val)
{
node *temp,*temp1,*back;
temp=first;
back=temp;
temp=first->link;
if(first==NULL)
{
cout<<"\nlist empty";
value=-1;
}
else
{
while(temp!=NULL)
{
if(temp1->data==val)
{
back->link=temp->link;
temp->link=NULL;
value=temp->data;
delete(temp);
break;
}
back=temp;
temp=temp->link;
}
}
return(first);
}
node*dellast(node*first,int &value)

VAIBHAV PANDEY Page 121


C++ Programming Class XI & XII 2013

{
node *temp;
node *temp1,*back;
temp=first;
if(first==NULL)
{
cout<<"\n\tlist empty";
value=-1;
}else
{
while(temp->link!=NULL)
{
back=temp;
temp=temp->link;
}
back->link=NULL;
value=temp->data;
delete(temp);
}
return(first);
}

INPUT:
enter how many nodes in the linked list:2
enter the data value of node 1:2
enter the data value of node 2:3
Main menu:
1.for deleting at first
2.for deleting in between
3.for deleting at last
4.traversing the list
5.for exit
enter tour choice:1
OUTPUT:
the deleted data is:2

VAIBHAV PANDEY Page 122


C++ Programming Class XI & XII 2013

Q.5 Program to perform all basic operation on stack the stack


contain data of integer type?
Date:4|10|12
Filename:lsq5.cpp

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
# define max 100
int stack[max];
int top;
void push(int stack[],int val,int &top);
int pop(int stack[],int &top);
void show_stack(int stack[],int top);
void main()
{
int choice,val;
char opt='y';
top=-1;
clrscr();
do
{
clrscr();
cout<<"\n main menu"
<<"\n1 Addition"
<<"\n2 Deletion"
<<"\n3 traverse"
<<"\n4 exit";
cout<<"\n enter your choice from above";
cin>>choice;
switch(choice)
{
case 1:

VAIBHAV PANDEY Page 123


C++ Programming Class XI & XII 2013

do
{
cout<<"\n enter the value to be added: ";
cin>>val;
push(stack,val,top);
cout<<"do u want more element(y/n)";
cin>>opt;
}
while(toupper(opt)=='y');
break;
case 2:
opt='y';
do
{
val=pop(stack,top);
if(val!=-1)
{
cout<<"value deleted from the stack"<<val;
cout<<"\ndo u want more element(y/n)";
cin>>opt;
}
}
while(toupper(opt)=='y');
break;
case 3:
show_stack(stack,top);
break;
case 4:
exit(0);
}
}
while(choice!=4);
}
void push (int stack[],int val,int&top)
{
if(top==max-1)

VAIBHAV PANDEY Page 124


C++ Programming Class XI & XII 2013

{
cout<<"\n\tstack full";
}
else
{
top=top+1;
stack[top]=val;
}
}
int pop (int stack[],int&top)
{
int value;
if(top<0)
{
cout<<"\n\tstack empty";
value=-1;
}
else
{
value=stack[top];
top=top-1;
}
return(value);
}
void show_stack(int stack[],int top)
{
int i;
if(top<0)
{
cout<<"\n\tstack empty";
return;
}
i=top;
clrscr();
cout<<"\n\tthe value are-->";
do

VAIBHAV PANDEY Page 125


C++ Programming Class XI & XII 2013

{
cout<<"\n\t"<<stack[i];
i=i-1;
}
while(i>=0);
getch();
}

OUTPUT:
main menu:
1.addition
2.deletion
3.traverse
4.exit
enter your chice:1
enter the value to be added :5
do you want to add more(y/n)->n

Q.6 Program to perform the basic operation of add stack ,delete


stack.The stack contains data of integers type.
Date:4|10|12
Filename:lsq6.cpp

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
struct node
{
int data;
node *link;
};
//function prototype of add stack,delete stack,and show stack in array impeletion.
node*push(node *top,int val);//add stack

VAIBHAV PANDEY Page 126


C++ Programming Class XI & XII 2013

node *pop(node*top,int &val);//delete stack


void show_stack(node*top);//show stack
void main()
{
node *top;
int choice, val;
char opt='Y';
top=NULL;
clrscr();
do
{
clrscr();
cout<<"\n\n\tMAIN MENU";
cout<<"\n\n\t1.ADDTION OF STACK";
cout<<"\n\n\t2.DELETION OF STACK";
cout<<"\n\n\t3.TRAVERSE OF STACK";
cout<<"\n\n\t4.EXIT FROM MENU";
cout<<"\n\nENTER YOUR CHOICE";
cin>>choice;
switch(choice)
{
case 1:
do
{
clrscr();
cout<<"\n\tENTER THE VALUE TO BE ADDED IN STACK-->";
cin>>val;
top=push(top,val);
cout<<"\n\tDO YOU WANT TO ADD MORE ELEMENT<Y/N>?";
cin>>opt;
}
while(toupper(opt)=='Y');
break;
case 2:
opt='Y';
do

VAIBHAV PANDEY Page 127


C++ Programming Class XI & XII 2013

{
top=pop(top,val);
if(val!=-1)
cout<<"\n\tVALUE DELETED FROM STACK IS-->"<<val;
cout<<"\n\tDO YOU WANT TO DELETE MORE ELEMENT<Y/N>?";
cin>>opt;
}
while (toupper(opt)=='Y');
break;
case 3:
show_stack(top);
break;
case 4:
exit(0);
}
}
while (choice!=4);
}
//FUNCTION BODY FOR ADD STACK WITH ARRAY
node *push(node*top,int val)
{
node *temp;
temp=new node;
temp->data=val;
temp->link=NULL;
if(top==NULL)
top=temp;
else
{
temp->link=top;
top=temp;
}
return(top);
}
//FUNCTION BODY FIR DELETE STACK WITH ARRAY
node *pop(node*top,int &val)

VAIBHAV PANDEY Page 128


C++ Programming Class XI & XII 2013

{
node*temp;
clrscr();
if (top==NULL)
{
cout<<"\n\t STACK EMPTY";
val=-1;
}
else
{
temp=top;
top=top->link;
val=temp->data;
temp->link=NULL;
delete temp;
}
return (top);
}
//FUNCTION BODY FOR SHOW STACK WITH ARRAY
void show_stack(node*top)
{
node *temp;
temp=top;
clrscr();
cout<<"\n\tTHE VALUE ARE-->";
while (temp!=NULL)
{
cout<<"THE VALUE ARE-->";
temp=temp->link;
}
getch();
}
INPUT:
MAIN MENU
1.ADDITION OF STACK
2.DELETION OF STACK

VAIBHAV PANDEY Page 129


C++ Programming Class XI & XII 2013

3.TRAVERSE OF STACK
4.EXIT FROM MENU
ENTER YOUR CHOICE 1
ENTER THE VALUE TO BE ADDED IN STACK-->34
DO YOU WANT TO ADD MORE ELEMENT<Y/N>?Y
ENTER YOUR CHOICE 3
OUTPUT:
THE VALUE ARE-->34

Q.7 Program to perform all basic operation of add queue, delete


queue and show queue?
Date:4|10|12
Filename:lsq7.cpp

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
# define max 100
int queue[max];
int top,front,rear;
void add_q(int queue[],int val,int &rear);
int del_q(int queue[],int &front,int rear);
void show_q(int queue[],int front,int rear);
void main()
{
int choice,val;
char opt='y';
rear=-1;
front=-1;
top=-1;
clrscr();
do
{

VAIBHAV PANDEY Page 130


C++ Programming Class XI & XII 2013

clrscr();
cout<<"\n main menu"
<<"\n1. addition"
<<"\n2. deletion"
<<"\n3. traverse"
<<"\n4. exit";
cout<<"\n enter your choice from above:";
cin>>choice;
switch(choice)
{
case 1:
do
{
cout<<"\n enter the value to be added :";
cin>>val;
add_q(queue,val,rear);
cout<<"do u want more element(y/n):";
cin>>opt;
}
while(toupper(opt)=='y');
break;
case 2:
opt='y';
do
{
val=del_q(queue,front,rear);
if(val!=-1)
cout<<"\nvalue deleted from the queue:"<<val;
cout<<"\ndo u want more element(y/n):";
cin>>opt;
}
while(toupper(opt)=='y');
break;
case 3:
show_q(queue,front,rear);
break;

VAIBHAV PANDEY Page 131


C++ Programming Class XI & XII 2013

case 4:
exit(0);
}
}
while(choice!=4);
}

void add_q(int queue[],int val,int &rear)


{
if(rear==max)
cout<<"\n \t Queue full!!!!";
else
{
rear=rear+1;
queue[rear]=val;
}
}

int del_q(int queue[],int &front,int rear)


{
int value;
if(front==rear)
{
cout<<"\n \t Queue empty!!!!";
value=-1;
}
else
{
front=front+1;
value=queue[front];
}
return(value);
}

void show_q(int queue[],int front,int rear)


{

VAIBHAV PANDEY Page 132


C++ Programming Class XI & XII 2013

clrscr();
if(front==rear)
{
cout<<"\n \t Queue empty!!!";
return;
}
else
{
cout<<"\nThe values are:";
do
{
front=front+1;
cout<<"\n"<<queue[front];
}
while(front!=rear);
getch();
}
}

OUTPUT:
main menu:
1.addition
2.deletion
3.traverse
4.exit
enter your choice from above:1
enter the value to be added:4
do u want to enter more(y/n):n

Q.8 Program to perform the basic operation of add queue,delete


queue,using linked list the queue contains
data of integer type?
Date:4|10|12
FIlename:lsq8.cpp

VAIBHAV PANDEY Page 133


C++ Programming Class XI & XII 2013

#include<ctype.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
struct node
{
int data;
node*link;
};
node*add_q(node*rear,int val);
node*del_q(node*front,int &val);
void show_q(node*front);
void main()
{
clrscr();
node *rear,*front;
int choice,val;
char opt='y';
front=rear=NULL;
do
{
clrscr ();
cout<<"\nmain menu";
cout<<"\n1.addition of queue";
cout<<"\n2.deletion from queue";
cout<<"\n3.traverse of queue";
cout<<"\n4.exit from menu";
cout<<"\nenter your choice from above:";
cin>>choice;
switch(choice)
{
case 1:
do
{
cout<<"\nenter data value to be added in the queue->";

VAIBHAV PANDEY Page 134


C++ Programming Class XI & XII 2013

cin>>val;
rear=add_q(rear,val);
if(front==NULL)
front=rear;
cout<<"\ndo you want to add more element<y/n>";
cin>>opt;
}
while(toupper(opt)=='y');
break;
case 2:
opt='y';
do
{
front=del_q(front,val);
if(front==NULL)
rear=front;
if(val!=-1)
cout<<"\nvalue deleted from queue is->"<<val;
cout<<"\ndo you want to delete more element:";
cin>>opt;
}
while(toupper(opt)=='y');
break;
case 3:
show_q(front);
case 4:
exit(0);
}
}
while(choice!=4);
}
node *add_q(node*rear,int val)
{
node*temp;
temp=new node;
temp->data=val;

VAIBHAV PANDEY Page 135


C++ Programming Class XI & XII 2013

temp->link=NULL;
if(rear==NULL)
rear=temp;
else
{
rear->link=temp;
rear=temp;
}
return(rear);
}
node *del_q(node *front,int &val)
{
node *temp;
clrscr();
temp=front;
front=front->link;
val=temp->data;
temp->link=NULL;
delete temp;
return (front);
}

void show_q(node *front)


{
node *temp;
temp=front;
clrscr();
cout<<"\nthe values are:";
while(temp!=NULL)
{
cout<<"\n"<<temp->data;
temp=temp->link;
}
getch();
}
OUTPUT:

VAIBHAV PANDEY Page 136


C++ Programming Class XI & XII 2013

main menu
1.addition of queue
2.deletion of queue
3.traverse of queue
4.exit from menu
enter your choice from above
enter data value to be added in queue-> 34
do you want to add more element<y/n>n
The values are:34

****************************************************
SQL
Q1. TABLE : STUDENT

DATE:12/11/2012
FILENAME:sq1_1.cpp

S.NO NAME STIPEND STREAM AVGMARKS GRADE CLASS


1 SOMESH 400.00 MEDICAL 78.5 B 12B
2 PRASHANT 450.00 COMMERCE 89.2 A 11C
3 TANI 300.00 COMMERCE 68.6 C 12C
4 ANYA 330.00 HUMANITIES 73.1 B 11A
5 SAM 500.00 NON-MEDICAL 90.6 A 12B
6 ABHI 410.00 MEDICAL 75.4 B 11A
7 GAURAV 250.00 HUMANITIES 64.4 C 12A

VAIBHAV PANDEY Page 137


C++ Programming Class XI & XII 2013

(A). Select all the non-medical stream student from student.


SQL> select*
from student
where stream=non-medical;
SNO NAME STIPEND STREAM AVGMARK GRADE CLASS
5 SAM 500 NON-MEDICAL 90.6 A 11A

(B). List all the names of students stored by stipend.


SQL> select name
from student
order by stipend;
NAME :
Gaurav

Tani

Anya

Somesh

Abhi

Prashant

Sam

(C). To count the number of student with grade A.


SQL> select count(*)
from student
where grade=A
count(*)
2

(D). To insert a new student and fill the columns with some value
SQL> insert new student
Values(7.Gaurav,250,Humanities,64.4,C,11A)

1 row created

VAIBHAV PANDEY Page 138


C++ Programming Class XI & XII 2013

(E). Give the output of the following

(i) SQL> select min(avgmark) from student


Where avgmark<75;
MIN(AVGMARK)
64.4
(ii) SQL> select sum(stipend) from student
Where grade=B;
SUM(STIPEND)
1140
(iii) SQL> select count (distinct(stream)) from student
COUNT (DISTINCT(STREAM))
4
(iv) SQL> select avg(stipend) from student
Where class=12B;
AVG(STIPEND)
450

*****************************************************

VAIBHAV PANDEY Page 139

S-ar putea să vă placă și