1. 1
1. WAP to print following series:
A A A
A A A
A A A
#include<iostream.h>
#include<conio.h>
int main()
{
int r=1;
while(r<=3)
{
int c=1;
while(c<=3)
{
cout<<"A"<<" ";
c++;
}
cout<<endl;
r++;
}
getch();
}
17. 9. WAP to create half diamond of the number which enters by user.
17
#include<iostream.h>
#include<conio.h>
int main()
{
int n;
cout<<"enter any number for create a half diamond";
cin>>n;
int r=1;
while(r<=n)
{
int k=n-1;
while(k>=r)
{
cout<<" ";
k--;
}
int c=1;
while(c<=r)
{
cout<<c;
20. 10. WAP to create a diamond of a number which enter by user.
20
#include<iostream.h>
#include<conio.h>
int main()
{
int n;
cout<<"enter any number for create a diamond";
cin>>n;
int r=1;
while(r<=n)
{
int k=n-1;
while(k>=r)
{
cout<<" ";
k--;
}
int c=1;
while(c<=r)
{
cout<<c;
21. 21
c++;
}
int m=r-1;
while(m>=1)
{
cout<<m;
m--;
}
cout<<endl;
r++;
}
int r1=n-1;
while(r1>=1)
{
int k=n-1;
while(k>=r1)
{
cout<<" ";
k--;
}
int c=1;
28. 13. WAP to print prime numbers out of 100 numbers.
28
#include<iostream.h>
#include<conio.h>
int main()
{
int j,i;
cout<<"The prime numbers are:"<<endl;
for (i=2; i<=100; i++)
for (j=2; j<=i; j++)
{
if(i%j == 0)
break;
else
{
cout<<i<<" ";
break;
}
}
getch();
}
30. 14. WAP to print Fibonacci series of given number.
30
#include<iostream.h>
#include<conio.h>
int main()
{
int x,y,z,n;
x=0;y=1;
cout<<"nEnter value of n:";
cin>>n;
cout<<"nFibonacci Series:nn";
cout<< x<<" ";
cout<< y<<" ";
for( int i=0; i<n-2; i++)
{
z = x+y;
cout<< z<<" ";
x = y;
y = z;
}
getch();
}
32. 15. WAP to print the table of a given number.
32
#include<iostream.h>
#include<conio.h>
int main()
{
int n;
cout<<"enter any number to print the table";
cin>>n;
for(int i=1;i<=10;i++)
{
int m=n*i;
cout<<n<<" "<<"*"<<" "<<i<<" "<<"="<<" "<<m<<endl;
}
getch();
}
34. 16. WAP to find out given number is even using if statement.
34
#include<iostream.h>
#include<conio.h>
int main()
{
int n;
cout<<"enter any number";
cin>>n;
if(n%2==0)
{
cout<<"Number is even";
}
getch();
}
36. 17. WAP to find out given number is even or odd using if else statement.
36
#include<iostream.h>
#include<conio.h>
int main()
{
int n;
cout<<"enter any number";
cin>>n;
if(n%2==0)
{
cout<<"Number is even";
}
else
{
cout<<"Number is odd";
}
getch();
}
38. 18. WAP to find out a largest number among three number using multiple if
statement.
38
#include<iostream.h>
#include<conio.h>
main()
{
int a,b,c;
cout<<"enter three no.'s ";
cin>>a>>b>>c;
if (a>b && a>c)
{
cout<<"largest number is "<<a;
}
if(b>a && b>c)
{
cout<<"largest number is "<<b;
}
if(c>a && c>b)
{
cout<<"largest number is "<<c;
}
40. 19. WAP to find out a largest number among three number using nested if
statement.
40
#include<iostream.h>
#include<conio.h>
int main()
{
int a,b,c;
cout<<"enter three numbers";
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
{
cout<<"greater number is "<<a;
}
else
{
cout<<"greater number is "<<c;
}
}
if(b>a)
41. 41
{
if(b>c)
{
cout<<"greater number is "<<b;
}
else
{
cout<<"greater number is "<<c;
}
}
getch();
}
43. 20. WAP to find out a largest number among three number using if else if
statement.
43
#include<iostream.h>
#include<conio.h>
main()
{
int a,b,c;
cout<<"enter three no.'s ";
cin>>a>>b>>c;
if (a>b && a>c)
{
cout<<"largest number is "<<a;
}
else if(b>a && b>c)
{
cout<<"largest number is "<<b;
}
else
{
cout<<"largest number is "<<c;
}
45. 21. WAP to illustrate the use of switch statement.
45
#include<iostream.h>
#include<conio.h>
int main()
{
int day;
cout<<"enter the number of day";
cin>>day;
switch(day)
{
case 0:
cout<<"Sunday";
break;
case 1:
cout<<"Monday";
break;
case 2:
cout<<"Tuesday";
break;
case 3:
cout<<"Wednesday";
46. 46
break;
case 4:
cout<<"Thursday";
break;
case 5:
cout<<"Friday";
break;
case 6:
cout<<"Saturday";
break;
default:
cout<<"Invalid choice try again";
break;
}
getch();
}
48. 22. WAP to find largest number out of 3 numbers using function without
argument and without return value.
48
#include<iostream.h>
#include<conio.h>
int main()
{
void largest(void);
largest();
getch();
}
void largest()
{
int x,y,z;
cout<<"enter three numbers:";
cin>>x>>y>>z;
if(x>y && x>z)
cout<<"largest number is "<<x;
else if(y>x && y>z)
cout<<"largest number is "<<y;
else
cout<<"largest number is "<<z;
50. 23. WAP to find largest number out of 3 numbers using function with
argument and without return value.
50
#include<iostream.h>
#include<conio.h>
int main()
{
void largest(int,int,int);
int a,b,c;
cout<<"enter three numbers:";
cin>>a>>b>>c;
largest(a,b,c);
getch();
}
void largest(int x,int y,int z)
{
if(x>y && x>z)
cout<<"largest number is "<<x;
else if(y>x && y>z)
cout<<"largest number is "<<y;
else
cout<<"largest number is "<<z;
52. 24. WAP to find largest number out of 3 numbers using function with
argument and with return value.
52
#include<iostream.h>
#include<conio.h>
int main()
{
int largest(int,int,int);
int a,b,c;
cout<<"enter three numbers:";
cin>>a>>b>>c;
int l=largest(a,b,c);
cout<<"largest number is "<<l;
getch();
}
int largest(int x,int y,int z)
{
if(x>y && x>z)
return(x);
else if(y>x && y>z)
return(y);
else
54. 25. WAP to enter the data of five students which include roll number of
student and marks in five subjects.
54
#include<iostream.h>
#include<conio.h>
class student
{
int rollno,m[5];
public:
void input();
void output();
};
void student::input()
{
cout<<"enter rollno";
cin>>rollno;
for(int i=0;i<=4;i++)
{
cout<<"enter marks for subject"<<i+1<<" ";
cin>>m[i];
}
}
55. 55
void student::output()
{
int p;
cout<<"rollno="<<rollno;
cout<<endl;
for(int i=0;i<=4;i++)
{
cout<<"marks of"<<i+1<<"subject"<<m[i];
cout<<endl;
}
}
int main()
{
student s[5];
for(int i=0;i<=4;i++)
{
cout<<"enter the data for student no"<<i+1<<endl;
s[i].input();
}
for(int i=0;i<=4;i++)
{
63. 28. WAP to illustrate the use of array of object and array within the class.
63
#include<iostream.h>
#include<conio.h>
class student
{
int rollno,m[5]; //array within class
public:
void input();
void output();
};
void student::input()
{
cout<<"enter rollno";
cin>>rollno;
for(int i=0;i<=4;i++)
{
cout<<"enter marks for subject"<<i+1<<" ";
cin>>m[i];
}
}
void student::output()
64. 64
{
int p;
cout<<"rollno="<<rollno;
cout<<endl;
for(int i=0;i<=4;i++)
{
cout<<"marks of"<<i+1<<"subject"<<m[i];
cout<<endl;
}
}
int main()
{
student s[5]; //array of object
for(int i=0;i<=4;i++)
{
cout<<"enter the data for student no"<<i+1<<endl;
s[i].input();
}
for(int i=0;i<=4;i++)
{
cout<<endl;
66. 29. WAP to print the total of diagonal of a matrix.
66
#include<iostream.h>
#include<conio.h>
int main()
{
int a[3][3], i, j,sum=0;
cout<<"nEnter elements of Array A:n";
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
cin>>a[i][j];
}
}
printf("nElements of Matrix A:n");
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
cout<<a[i][j]<<"t";
}
69. 69
30. WAP to addition of two matrices.
#include<iostream.h>
#include<conio.h>
int main()
{
int a[3][3], b[3][3], c[3][3], i, j;
cout<<"nEnter elements of Array A:n";
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
cin>>a[i][j];
}
}
cout<<"nEnter elements of Array B:n";
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
cin>>b[i][j];
70. 70
}
}
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
printf("nElements of Matrix A:n");
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
cout<<a[i][j]<<"t";
}
cout<<endl;
}
printf("nElements of Matrix b:n");
for (i=0; i<3; i++)
{
71. 71
for (j=0; j<3; j++)
{
cout<<b[i][j]<<"t";
}
cout<<endl;
}
cout<<"nMatrix Addition is:n";
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
cout<<c[i][j]<<"t";;
}
cout<<endl;
}
getch();
}
73. 31. WAP to illustrate the use of single inheritance.
73
#include<iostream.h>
#include<conio.h>
class a
{
private:
void f1()
{
cout<<"I am f1"<<endl;
}
public:
void f2()
{
cout<<"I am f2"<<endl;
}
};
class b:public a
{
private:
void f3()
{
74. 74
cout<<"I am f3"<<endl;
}
public:
void f4()
{
cout<<"I am f4"<<endl;
}
};
int main()
{
b o1;
o1.f2();
o1.f4();
getch();
}
76. 32. WAP to illustrate the use of multiple inheritances.
76
#include<iostream.h>
#include<conio.h>
class a
{
private:
void f1()
{
cout<<"I am f1"<<endl;
}
public:
void f2()
{
cout<<"I am f2"<<endl;
}
};
class b
{
private:
void f3()
{
77. 77
cout<<"I am f3"<<endl;
}
public:
void f4()
{
cout<<"I am f4"<<endl;
}
};
class c:public a,public b
{
private:
void f5()
{
cout<<"I am f5"<<endl;
}
public:
void f6()
{
cout<<"I am f6"<<endl;
}
};
78. 78
int main()
{
c o1;
o1.f2();
o1.f4();
o1.f6();
getch();
}
Output:
79. 33. WAP to illustrate the use of multilevel inheritance.
79
#include<iostream.h>
#include<conio.h>
class a
{
private:
void f1()
{
cout<<"I am f1"<<endl;
}
public:
void f2()
{
cout<<"I am f2"<<endl;
}
};
class b:public a
{
private:
void f3()
{
80. 80
cout<<"I am f3"<<endl;
}
public:
void f4()
{
cout<<"I am f4"<<endl;
}
};
class c:public b
{
private:
void f5()
{
cout<<"I am f5"<<endl;
}
public:
void f6()
{
cout<<"I am f6"<<endl;
}
};
81. 81
int main()
{
b o1;
c o2;
o1.f2();
o1.f4();
o2.f6();
getch();
}
Output:
82. 34. WAP to illustrate the use of hierarchical inheritance.
82
#include<iostream.h>
#include<conio.h>
class a
{
private:
void f1()
{
cout<<"I am f1"<<endl;
}
public:
void f2()
{
cout<<" I am f2"<<endl;
}
};
class b:public a
{
private:
void f3()
{
83. 83
cout<<" I am f3"<<endl;
}
public:
void f4()
{
cout<<" I am f4"<<endl;
}
};
class c:public a
{
private:
void f5()
{
cout<<" I am f5";
}
public:
void f6()
{
cout<<" I am f6";
}
};
84. 84
int main()
{
b o1;
c o2;
o1.f2();
o1.f4();
o2.f2();
o2.f6();
getch();
}
Output:
85. 35. WAP to illustrate the use of hybrid inheritance.
85
#include<iostream.h>
#include<conio.h>
class a
{
private:
void f1()
{
cout<<"I am f1"<<endl;
}
public:
void f2()
{
cout<<"I am f2"<<endl;
}
};
class b:virtual public a
{
private:
void f3()
{
86. 86
cout<<"I am f3"<<endl;
}
public:
void f4()
{
cout<<"I am f4"<<endl;
}
};
class c:virtual public a
{
private:
void f5()
{
cout<<"I am f5"<<endl;
}
public:
void f6()
{
cout<<"I am f6"<<endl;
}
};
87. 87
class d:public b,public c
{
private:
void f7()
{
cout<<"I am f7"<<endl;
}
public:
void f8()
{
cout<<"I am f8"<<endl;
}
};
int main()
{
d o1;
o1.f2();
o1.f4();
o1.f2();
o1.f6();
o1.f8();
91. 37. WAP to demonstrate the use of destructor.
91
#include<iostream.h>
#include<conio.h>
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout<<"number of object created"<<count<<endl;
}
~alpha()
{
cout<<"number of object destroyed"<<count<<endl;
count--;
}
};
int main()
{
cout<<"enter main:"<<endl;
97. 39. WAP to illustrate the use of function overloading.
97
#include<iostream.h>
#include<conio.h>
int operate(int a,int b)
{
return(a*b);
}
float operate(float a, float b)
{
return(a/b);
}
int main()
{
int x=10,y=20;
float m=2.9,n=1.2;
cout<<"multiply=="<<operate(x,y)<<endl;
cout<<"devision=="<<operate(m,n);
getch();
}
106. 42. WAP to illustrate the use of friend function.
106
#include<iostream.h>
#include<conio.h>
class Point
{
int m_i;
friend void ChangePrivate( Point & );
public:
Point( void )
{
m_i=0;
}
void PrintPrivate( void )
{
cout << m_i << endl;
}
};
void ChangePrivate ( Point &i )
{
i.m_i++;
}
107. 107
int main()
{
Point sPoint;
sPoint.PrintPrivate();
ChangePrivate(sPoint);
sPoint.PrintPrivate();
getch();
}
Output:
108. 43. WAP to illustrate the use of virtual function.
108
#include<iostream.h>
#include<conio.h>
class b
{
public:
void virtual show()
{
cout<<"i belong to base class";
}
};
class d:public b
{
public:
void show()
{
cout<<"i belong to derived class";
}
};
int main()
{
109. 109
b *b1;
d d1;
b1=&d1;
b1->show();
getch();
}
Output:
110. 44. WAP to illustrate the use of inline function.
110
#include<iostream.h>
#include<conio.h>
class abc
{
public:
inline void show()
{
cout<<"Hello"<<endl;
}
};
int main()
{
abc obj[3];
for(int i=0;i<3;i++)
{
obj[i].show();
}
getch();
}
112. 45. WAP to illustrate the use of static data member and static member
function.
112
#include<iostream.h>
#include<conio.h>
class test
{
int code;
static int count;
public:
void setcode()
{
code=++count;
}
void showcode()
{
cout<<"object number:"<<code<<"n";
}
static void showcount()
{
cout<<"count:"<<count<<endl;
}
113. 113
};
int test::count;
int main()
{
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
}
115. 115
46. WAP to write in a text file.
#include<fstream.h>
#include<iostream.h>
int main()
{
ofstream fout;
fout.open("out.txt");
char str[300]="Time is a great teacher but unfortunately it kills all its pupils. Berlioz";
fout<<str;
fout.close();
return 0;
}
Output:
116. 47. WAP to read from text files and display it.
116
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
int main()
{
ifstream fin;
fin.open("out.txt");
char ch;
while(!fin.eof())
{
fin.get(ch);
cout<<ch;
}
fin.close();
getch();
}
118. 48. WAP to count number of character in text file.
118
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
int main()
{
ifstream fin;
fin.open("out.txt");
char ch;
int count=0;
while(!fin.eof())
{
fin.get(ch);
count++;
}
cout<<"Number of characters in file is "<<count;
fin.close();
getch();
return 0;
120. 49. WAP to count number of words in text file.
120
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
int main()
{
ifstream fin;
fin.open("out.txt");
char word[30]; int count=0;
while(!fin.eof())
{
fin>>word;
count++;
}
cout<<"Number of words in file is "<<count;
fin.close();
getch();
return 0;
}
122. 50. WAP to count number of lines in text file.
122
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
int main()
{
ifstream fin;
fin.open("out.txt");
char str[80]; int count=0;
while(!fin.eof())
{
fin.getline(str,80);
count++;
}
cout<<"Number of lines in file is "<<count;
fin.close();
getch();
return 0;
}