Documente Academic
Documente Profesional
Documente Cultură
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>
#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
#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;
}
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
#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
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
#include<iostream.h>
#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
#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(+,-,*,/): *
OUTPUT:
The calculated result is: 21
#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
#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
#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
#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
{
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
#include<iostream.h>
#include<conio.h>
int main()
{
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
#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
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
#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
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
#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];
}
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;
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
#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;
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.
#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;
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
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float marks[4][3],sum,avg;
char grade[4];
int i,j;
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
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
#include<iostream.h>
#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!!!
#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++)
{
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
#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++)
{
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
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:
The cube of 3 is 27
#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
#include<iostream.h>
#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();
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
#include<iostream.h>
#include<conio.h>
#include<process.h>
int main()
{
clrscr();
OUTPUT:
Distance=180 I
#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;
}
}
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
#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)
{
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
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;
}
}
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
#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
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
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
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
#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
#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
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
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
#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;
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
#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
#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
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct addr //global definition
{
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;
}
OUTPUT :
Enter employee number : 103
Name:neel
Designation:Manager
Enter address:
house no:321
Area:Sadiq,Nagar
City:delhi
State:Delhi
Basic Pay:9500
#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;
}
OUTPUT:
Total feet:10
Total inches:9
#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];
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];
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
city:gotan
State:rajasthan
pincode:342902
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;
};
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
#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;
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::
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
void main()
{
clrscr();
char ch;
cout<<"\nEnter the character:\n";
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.
#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]))
{
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();
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);
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.
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
#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
::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
//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
#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
#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
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
#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;
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
#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;
}
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;
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
#include<iostream.h>
#include<stdio.h> //for gets()
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
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.
#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:
OUTPUT :
Constructing Base2
Constructing Base1
Constructing Derived.
1. 15 2. 14 3. 16
destructing Derived.
destructing Base1
destructing Base2
*********************************************************
CONSTRUCTOR AND DESTRUCTOR
#include<iostream.h>
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;
}
#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
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
#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;
}
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
#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);
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>
#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 :
#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;
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
#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];
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;
}
return pos;
}
INPUT :
OUTPUT :
The arrray now is as shown below...
2 4 6 9 10 11 12 15
#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++)
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;
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
#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";
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
#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;
}
}
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
OUTPUT :
The Sorted array is as shown below....
4 6 7 9
#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
OUTPUT :
The sorted array is as shown below....
1 2 3 4 6 9 34
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 matrix - A
12
34
Input matrix - B
43
21
OUTPUT :
The sum of two matrices is :
55
55
#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++)
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 :
#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)
{
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 :
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
}
#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
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
#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
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
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
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
}
INPUT:
Enter Rollno. : 101
Enter Marks : 78
Want to enter more records?(y/n)....y
#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:";
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 garde :B
Enter marks :66
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
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()
{
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
3 91
#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++)
{ 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
#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
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
#include<iostream.h>
#include<string.h>
void main()
{
char*names[]={"sachin","kaoil","ajay","sunil","anil};
int len=0;
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
#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";
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
#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)
{
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
#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;
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 :
1
2
3
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
OUTPUT:
The linked list value are:
2
7
data value to be searched->2
search is suceesfull
#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->";
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;
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;
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
3. Inserting at last
4. traversing at last
5. exit
enter your choice-> 5
#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:";
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 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;
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)
{
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
#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:
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)
{
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
{
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
#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
{
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)
{
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
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
#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
{
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;
case 4:
exit(0);
}
}
while(choice!=4);
}
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
#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->";
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;
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);
}
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
Tani
Anya
Somesh
Abhi
Prashant
Sam
(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
*****************************************************