SlideShare une entreprise Scribd logo
1  sur  125
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(); 
}
2 
Output:
3 
2. WAP to print following series: 
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1 
#include<iostream.h> 
#include<conio.h> 
int main() 
{ 
int r=5; 
while(r>=1) 
{ 
int c=r; 
while(c>=1) 
{ 
cout<<c<<" "; 
c--; 
} 
cout<<endl;
4 
r--; 
} 
getch(); 
} 
Output:
5 
3. WAP to print following series: 
5 4 3 2 1 
5 4 3 2 
5 4 3 
5 4 
5 
#include<iostream.h> 
#include<conio.h> 
int main() 
{ 
int r=1; 
while(r<=5) 
{ 
int c=5; 
while(c>=r) 
{ 
cout<<c<<" "; 
c--; 
} 
cout<<endl;
6 
r++; 
} 
getch(); 
} 
Output:
7 
4. WAP to print following series: 
5 
5 4 
5 4 3 
5 4 3 2 
5 4 3 2 1 
#include<iostream.h> 
#include<conio.h> 
int main() 
{ 
int r=5; 
while(r>=1) 
{ 
int c=5; 
while(c>=r) 
{ 
cout<<c<<" "; 
c--; 
} 
cout<<endl;
8 
r--; 
} 
getch(); 
} 
Output:
9 
5. WAP to print following series: 
1 
2 3 
4 5 6 
7 8 9 10 
#include<iostream.h> 
#include<conio.h> 
int main() 
{ 
int k=1; 
int r=1; 
while(r<=4) 
{ 
int c=1; 
while(c<=r) 
{ 
cout<<k<<" "; 
c++; 
k++; 
} 
cout<<endl;
10 
r++; 
} 
getch(); 
} 
Output:
11 
6. WAP to print following series: 
1 
1 2 
2 3 4 
4 5 6 7 
7 8 9 10 11 
#include<iostream.h> 
#include<conio.h> 
int main() 
{ 
int k=1; 
int r=1; 
while(r<=5) 
{ 
int c=1; 
while(c<=r) 
{ 
cout<<k<<" "; 
c++; 
k++;
12 
} 
cout<<endl; 
r++; 
k--; 
} 
getch(); 
} 
Output:
13 
7. WAP to print following series: 
1 2 3 4 
5 6 7 
8 9 
10 
#include<iostream.h> 
#include<conio.h> 
int main() 
{ 
int k=1; 
int r=4; 
while(r>=1) 
{ 
int c=1; 
while(c<=r) 
{ 
cout<<k<<" "; 
c++; 
k++; 
} 
cout<<endl;
14 
r--; 
} 
getch(); 
} 
Output:
15 
8. WAP to print following series: 
10 
9 8 
7 6 5 
4 3 2 1 
#include<iostream.h> 
#include<conio.h> 
int main() 
{ 
int k=10; 
int r=1; 
while(r<=4) 
{ 
int c=1; 
while(c<=r) 
{ 
cout<<k<<" "; 
c++; 
k--; 
} 
cout<<endl;
16 
r++; 
} 
getch (); 
} 
Output:
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;
18 
c++; 
} 
int m=r-1; 
while(m>=1) 
{ 
cout<<m; 
m--; 
} 
cout<<endl; 
r++; 
} 
getch(); 
}
19 
Output:
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 
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;
22 
while(c<=r1) 
{ 
cout<<c; 
c++; 
} 
int m=r1-1; 
while(m>=1) 
{ 
cout<<m; 
m--; 
} 
cout<<endl; 
r1--; 
} 
getch(); 
}
23 
Output:
11. WAP to print Armstrong numbers out of 1000 numbers. 
24 
#include<iostream.h> 
#include<conio.h> 
int main() 
{ 
int n; 
for(n=1;n<=1000;n++) 
{ 
int temp,d,arm=0; 
temp=n; 
while(temp>0) 
{ 
d=temp%10; 
temp=temp/10; 
arm=arm+(d*d*d); 
} 
if(arm==n) 
{ 
cout<<n; 
cout<<endl; 
}
25 
} 
getch(); 
} 
Output:
12. WAP to find factorial of given number. 
26 
# include<iostream.h> 
#include<conio.h> 
int main() 
{ 
int n,i,fact=1; 
cout<<"Enter number"; 
cin>>n; 
for(i=1;i<=n;i++) 
{ 
fact=fact*i; 
} 
cout<<"factorial="<<fact; 
getch(); 
}
27 
Output:
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(); 
}
29 
Output:
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(); 
}
31 
Output:
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(); 
}
33 
Output:
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(); 
}
35 
Output:
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(); 
}
37 
Output:
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; 
}
39 
getch(); 
} 
Output:
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 
{ 
if(b>c) 
{ 
cout<<"greater number is "<<b; 
} 
else 
{ 
cout<<"greater number is "<<c; 
} 
} 
getch(); 
}
42 
Output:
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; 
}
44 
getch(); 
} 
Output:
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 
break; 
case 4: 
cout<<"Thursday"; 
break; 
case 5: 
cout<<"Friday"; 
break; 
case 6: 
cout<<"Saturday"; 
break; 
default: 
cout<<"Invalid choice try again"; 
break; 
} 
getch(); 
}
47 
Output:
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;
49 
} 
Output:
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;
51 
} 
Output:
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
53 
return(z); 
} 
Output:
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 
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++) 
{
56 
cout<<endl; 
cout<<"showing data for student no"<<i+1<<endl; 
s[i].output(); 
} 
getch(); 
} 
Output:
26. WAP using function definition inside the class. 
57 
#include<iostream.h> 
#include<conio.h> 
class student 
{ 
private: 
int rollno; 
char name[20]; 
char add[50]; 
public: 
void getdata() 
{ 
cout<<"enter Roll number, name and add:"<<endl; 
cin>>rollno; 
cin>>name; 
cin>>add; 
} 
void putdata() 
{ 
cout<<rollno<<" "; 
cout<<name<<" ";
58 
cout<<add<<" "; 
} 
}; 
int main() 
{ 
student o1[5]; 
for(int i=0;i<5;i++) 
{ 
o1[i].getdata(); 
} 
cout<<"Rollno"<<"t"<<"Name"<<"t"<<"Address"<<"t"<<endl; 
for(int i=0;i<5;i++) 
{ 
o1[i].putdata(); 
cout<<endl; 
} 
getch(); 
}
59 
Output:
27. WAP using function definition outside the class. 
60 
#include<iostream.h> 
#include<conio.h> 
class student 
{ 
int rollno; 
char name[20]; 
char add[50]; 
public: 
void getdata(); 
void putdata(); 
}; 
void student :: getdata() 
{ 
cout<<"enter Roll number,name and add:"<<endl; 
cin>>rollno; 
cin>>name; 
cin>>add; 
} 
void student :: putdata() 
{
61 
cout<<rollno<<" "; 
cout<<name<<" "; 
cout<<add<<" "; 
} 
int main() 
{ 
student o1[5]; 
for(int i=0;i<5;i++) 
{ 
o1[i].getdata(); 
} 
cout<<"Rollno"<<"t"<<"Name"<<"t"<<"Address"<<"t"<<endl; 
for(int i=0;i<5;i++) 
{ 
o1[i].putdata(); 
cout<<endl; 
} 
getch(); 
}
62 
Output:
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 
{ 
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;
cout<<"showing data for student no"<<i+1<<endl; 
65 
s[i].output(); 
} 
getch(); 
} 
Output:
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"; 
}
67 
cout<<endl; 
} 
for (i=0; i<3; i++) 
{ 
for (j=0; j<3; j++) 
{ 
if(i==j) 
sum=sum+a[i][j]; 
} 
} 
cout<<"nMatrix diagnal Addition is:n"<<sum; 
getch(); 
}
68 
Output:
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 
} 
} 
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 
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(); 
}
72 
Output:
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 
cout<<"I am f3"<<endl; 
} 
public: 
void f4() 
{ 
cout<<"I am f4"<<endl; 
} 
}; 
int main() 
{ 
b o1; 
o1.f2(); 
o1.f4(); 
getch(); 
}
75 
Output:
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 
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 
int main() 
{ 
c o1; 
o1.f2(); 
o1.f4(); 
o1.f6(); 
getch(); 
} 
Output:
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 
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 
int main() 
{ 
b o1; 
c o2; 
o1.f2(); 
o1.f4(); 
o2.f6(); 
getch(); 
} 
Output:
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 
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 
int main() 
{ 
b o1; 
c o2; 
o1.f2(); 
o1.f4(); 
o2.f2(); 
o2.f6(); 
getch(); 
} 
Output:
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 
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 
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();
88 
getch(); 
} 
Output:
36. WAP to demonstrate the use of parameterized constructor and default 
constructor. 
89 
#include<iostream.h> 
#include<conio.h> 
class integer 
{ 
int m,n; 
public: 
integer() //Default constructor 
{ 
m=0; 
n=0; 
} 
integer(int x,int y) //Parameterized constructor 
{ 
m=x; 
n=y; 
} 
void display(void) 
{ 
cout<<"value1="<<m<<endl;
90 
cout<<"value2="<<n<<endl; 
} 
}; 
int main() 
{ 
integer o1,o2(10,20); 
cout<<"Default constructor:"<<endl; 
o1.display(); 
cout<<"Parameterized constructor:"<<endl; 
o2.display(); 
getch(); 
} 
Output:
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;
92 
{ 
alpha a1,a2,a3,a4; 
{ 
cout<<"enter block1:"<<endl; 
alpha a5; 
} 
{ 
cout<<"enter block2:"<<endl; 
alpha a6; 
} 
cout<<"re-enter in main:"<<endl; 
} 
getch(); 
}
93 
Output:
38. WAP to illustrate the use of copy constructor. 
94 
#include<iostream.h> 
#include<conio.h> 
class code 
{ 
int id; 
public: 
code() 
{ 
} 
code(int a) 
{ 
id=a; 
} 
code(code &i) 
{ 
id=i.id; 
} 
void display(void) 
{ 
cout<<"id="<<id<<endl;
95 
} 
}; 
int main() 
{ 
code a(100); 
code b=a; 
code c(a); 
code d; 
d=a; 
a.display(); 
b.display(); 
c.display(); 
d.display(); 
getch(); 
}
96 
Output:
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(); 
}
98 
Output:
40. WAP to illustrate the use of unary operator overloading. 
99 
#include<iostream.h> 
#include<conio.h> 
class single 
{ 
int a,b; 
public: 
single() 
{ 
a=0; 
b=0; 
} 
single(int x,int y) 
{ 
a=x; 
b=y; 
} 
void display() 
{ 
cout<<"A="<<a<<endl;
100 
cout<<"B="<<b<<endl; 
} 
single operator-() 
{ 
a=-a; 
b=-b; 
} 
}; 
int main() 
{ 
single o1; 
single o2(10,20); 
-o1; 
-o2; 
o1.display(); 
o2.display(); 
getch(); 
}
101 
Output:
41. WAP to illustrate the use of binary operator overloading. 
102 
#include<iostream.h> 
#include<conio.h> 
class abc 
{ 
int a,b; 
public: 
abc() 
{ 
a=0; 
b=0; 
} 
abc(int x,int y) 
{ 
a=x; 
b=y; 
} 
void display() 
{ 
cout<<"a="<<a; 
cout<<"tb="<<b<<endl;
103 
} 
abc operator +(abc &n) 
{ 
abc temp; 
temp.a=a+n.a; 
temp.b=b+n.b; 
return(temp); 
} 
abc operator *(abc &n) 
{ 
abc temp; 
temp.a= a * n.a; 
temp.b= b * n.b; 
return(temp); 
} 
abc operator /(abc &n) 
{ 
abc temp; 
temp.a= a / n.a; 
temp.b= b / n.b; 
return(temp);
104 
} 
}; 
int main() 
{ 
abc a1(30,40); 
abc a2(10,20); 
abc a3,a4,a5; 
a3=a1+a2; 
a4=a1*a2; 
a5=a1/a2; 
a1.display(); 
a2.display(); 
cout<<"ADDTION"<<endl; 
a3.display(); 
cout<<"SUBTRACTION"<<endl; 
a4.display(); 
cout<<"DEVISION"<<endl; 
a5.display(); 
getch(); 
}
105 
Output:
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 
int main() 
{ 
Point sPoint; 
sPoint.PrintPrivate(); 
ChangePrivate(sPoint); 
sPoint.PrintPrivate(); 
getch(); 
} 
Output:
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 
b *b1; 
d d1; 
b1=&d1; 
b1->show(); 
getch(); 
} 
Output:
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(); 
}
111 
Output:
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 
}; 
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(); 
}
114 
Output:
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:
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(); 
}
117 
Output:
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;
119 
} 
Output:
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; 
}
121 
Output:
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; 
}
123 
Output:
51. WAP to copy contents of one file to another file. 
124 
#include<fstream.h> 
int main() 
{ 
ifstream fin; 
fin.open("out.txt"); 
ofstream fout; 
fout.open("sample.txt"); 
char ch; 
while(!fin.eof()) 
{ 
fin.get(ch); 
fout<<ch; 
} 
fin.close(); 
return 0; 
}
125 
Output:

Contenu connexe

Tendances (20)

C++ TUTORIAL 10
C++ TUTORIAL 10C++ TUTORIAL 10
C++ TUTORIAL 10
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
C++ file
C++ fileC++ file
C++ file
 
C++ TUTORIAL 9
C++ TUTORIAL 9C++ TUTORIAL 9
C++ TUTORIAL 9
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
Oops practical file
Oops practical fileOops practical file
Oops practical file
 
C++ TUTORIAL 7
C++ TUTORIAL 7C++ TUTORIAL 7
C++ TUTORIAL 7
 
Bijender (1)
Bijender (1)Bijender (1)
Bijender (1)
 
Network lab manual
Network lab manualNetwork lab manual
Network lab manual
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
10 template code program
10 template code program10 template code program
10 template code program
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
C- Programs - Harsh
C- Programs - HarshC- Programs - Harsh
C- Programs - Harsh
 
C++ Programming - 3rd Study
C++ Programming - 3rd StudyC++ Programming - 3rd Study
C++ Programming - 3rd Study
 
C++ TUTORIAL 6
C++ TUTORIAL 6C++ TUTORIAL 6
C++ TUTORIAL 6
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 

En vedette (17)

C file
C fileC file
C file
 
Target Audience
Target AudienceTarget Audience
Target Audience
 
Conventional representation
Conventional representationConventional representation
Conventional representation
 
Repositorio digital
Repositorio digitalRepositorio digital
Repositorio digital
 
Institutions Research
Institutions ResearchInstitutions Research
Institutions Research
 
Latihan Matematik
Latihan MatematikLatihan Matematik
Latihan Matematik
 
Departments and its functions
Departments and its functionsDepartments and its functions
Departments and its functions
 
Lighting
LightingLighting
Lighting
 
Step 1
Step 1Step 1
Step 1
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentation
 
Citylights jan 2016
Citylights jan 2016Citylights jan 2016
Citylights jan 2016
 
Geometrical tolerance 2014
Geometrical tolerance 2014Geometrical tolerance 2014
Geometrical tolerance 2014
 
Java file
Java fileJava file
Java file
 
Evaluation
EvaluationEvaluation
Evaluation
 
Diapo ingles
Diapo inglesDiapo ingles
Diapo ingles
 
Dracula
DraculaDracula
Dracula
 
Half term quiz
Half term quizHalf term quiz
Half term quiz
 

Similaire à C++ Programming Series Examples

12th CBSE Practical File
12th CBSE Practical File12th CBSE Practical File
12th CBSE Practical FileAshwin Francis
 
Assignment no 5
Assignment no 5Assignment no 5
Assignment no 5nancydrews
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxssuser3cbb4c
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyGrejoJoby1
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloadingkinan keshkeh
 
Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Syed Umair
 
Assignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.zAssignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.zSyed Umair
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_papervandna123
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solvingSyed Umair
 

Similaire à C++ Programming Series Examples (20)

C++ file
C++ fileC++ file
C++ file
 
C++
C++C++
C++
 
12th CBSE Practical File
12th CBSE Practical File12th CBSE Practical File
12th CBSE Practical File
 
Assignment no 5
Assignment no 5Assignment no 5
Assignment no 5
 
Oop1
Oop1Oop1
Oop1
 
Final DAA_prints.pdf
Final DAA_prints.pdfFinal DAA_prints.pdf
Final DAA_prints.pdf
 
7720
77207720
7720
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
C++ practical
C++ practicalC++ practical
C++ practical
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)
 
Statement
StatementStatement
Statement
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Assignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.zAssignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.z
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solving
 

Plus de simarsimmygrewal

Plus de simarsimmygrewal (7)

Java file
Java fileJava file
Java file
 
C file
C fileC file
C file
 
Handling inputs via scanner class
Handling inputs via scanner classHandling inputs via scanner class
Handling inputs via scanner class
 
Handling inputs via io..continue
Handling inputs via io..continueHandling inputs via io..continue
Handling inputs via io..continue
 
Excception handling
Excception handlingExcception handling
Excception handling
 
Type casting
Type castingType casting
Type casting
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 

Dernier

UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 

Dernier (20)

UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 

C++ Programming Series Examples

  • 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(); }
  • 3. 3 2. WAP to print following series: 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 #include<iostream.h> #include<conio.h> int main() { int r=5; while(r>=1) { int c=r; while(c>=1) { cout<<c<<" "; c--; } cout<<endl;
  • 4. 4 r--; } getch(); } Output:
  • 5. 5 3. WAP to print following series: 5 4 3 2 1 5 4 3 2 5 4 3 5 4 5 #include<iostream.h> #include<conio.h> int main() { int r=1; while(r<=5) { int c=5; while(c>=r) { cout<<c<<" "; c--; } cout<<endl;
  • 6. 6 r++; } getch(); } Output:
  • 7. 7 4. WAP to print following series: 5 5 4 5 4 3 5 4 3 2 5 4 3 2 1 #include<iostream.h> #include<conio.h> int main() { int r=5; while(r>=1) { int c=5; while(c>=r) { cout<<c<<" "; c--; } cout<<endl;
  • 8. 8 r--; } getch(); } Output:
  • 9. 9 5. WAP to print following series: 1 2 3 4 5 6 7 8 9 10 #include<iostream.h> #include<conio.h> int main() { int k=1; int r=1; while(r<=4) { int c=1; while(c<=r) { cout<<k<<" "; c++; k++; } cout<<endl;
  • 10. 10 r++; } getch(); } Output:
  • 11. 11 6. WAP to print following series: 1 1 2 2 3 4 4 5 6 7 7 8 9 10 11 #include<iostream.h> #include<conio.h> int main() { int k=1; int r=1; while(r<=5) { int c=1; while(c<=r) { cout<<k<<" "; c++; k++;
  • 12. 12 } cout<<endl; r++; k--; } getch(); } Output:
  • 13. 13 7. WAP to print following series: 1 2 3 4 5 6 7 8 9 10 #include<iostream.h> #include<conio.h> int main() { int k=1; int r=4; while(r>=1) { int c=1; while(c<=r) { cout<<k<<" "; c++; k++; } cout<<endl;
  • 14. 14 r--; } getch(); } Output:
  • 15. 15 8. WAP to print following series: 10 9 8 7 6 5 4 3 2 1 #include<iostream.h> #include<conio.h> int main() { int k=10; int r=1; while(r<=4) { int c=1; while(c<=r) { cout<<k<<" "; c++; k--; } cout<<endl;
  • 16. 16 r++; } getch (); } Output:
  • 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;
  • 18. 18 c++; } int m=r-1; while(m>=1) { cout<<m; m--; } cout<<endl; r++; } getch(); }
  • 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;
  • 22. 22 while(c<=r1) { cout<<c; c++; } int m=r1-1; while(m>=1) { cout<<m; m--; } cout<<endl; r1--; } getch(); }
  • 24. 11. WAP to print Armstrong numbers out of 1000 numbers. 24 #include<iostream.h> #include<conio.h> int main() { int n; for(n=1;n<=1000;n++) { int temp,d,arm=0; temp=n; while(temp>0) { d=temp%10; temp=temp/10; arm=arm+(d*d*d); } if(arm==n) { cout<<n; cout<<endl; }
  • 25. 25 } getch(); } Output:
  • 26. 12. WAP to find factorial of given number. 26 # include<iostream.h> #include<conio.h> int main() { int n,i,fact=1; cout<<"Enter number"; cin>>n; for(i=1;i<=n;i++) { fact=fact*i; } cout<<"factorial="<<fact; getch(); }
  • 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; }
  • 39. 39 getch(); } Output:
  • 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; }
  • 44. 44 getch(); } Output:
  • 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
  • 53. 53 return(z); } Output:
  • 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++) {
  • 56. 56 cout<<endl; cout<<"showing data for student no"<<i+1<<endl; s[i].output(); } getch(); } Output:
  • 57. 26. WAP using function definition inside the class. 57 #include<iostream.h> #include<conio.h> class student { private: int rollno; char name[20]; char add[50]; public: void getdata() { cout<<"enter Roll number, name and add:"<<endl; cin>>rollno; cin>>name; cin>>add; } void putdata() { cout<<rollno<<" "; cout<<name<<" ";
  • 58. 58 cout<<add<<" "; } }; int main() { student o1[5]; for(int i=0;i<5;i++) { o1[i].getdata(); } cout<<"Rollno"<<"t"<<"Name"<<"t"<<"Address"<<"t"<<endl; for(int i=0;i<5;i++) { o1[i].putdata(); cout<<endl; } getch(); }
  • 60. 27. WAP using function definition outside the class. 60 #include<iostream.h> #include<conio.h> class student { int rollno; char name[20]; char add[50]; public: void getdata(); void putdata(); }; void student :: getdata() { cout<<"enter Roll number,name and add:"<<endl; cin>>rollno; cin>>name; cin>>add; } void student :: putdata() {
  • 61. 61 cout<<rollno<<" "; cout<<name<<" "; cout<<add<<" "; } int main() { student o1[5]; for(int i=0;i<5;i++) { o1[i].getdata(); } cout<<"Rollno"<<"t"<<"Name"<<"t"<<"Address"<<"t"<<endl; for(int i=0;i<5;i++) { o1[i].putdata(); cout<<endl; } getch(); }
  • 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;
  • 65. cout<<"showing data for student no"<<i+1<<endl; 65 s[i].output(); } getch(); } Output:
  • 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"; }
  • 67. 67 cout<<endl; } for (i=0; i<3; i++) { for (j=0; j<3; j++) { if(i==j) sum=sum+a[i][j]; } } cout<<"nMatrix diagnal Addition is:n"<<sum; getch(); }
  • 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();
  • 88. 88 getch(); } Output:
  • 89. 36. WAP to demonstrate the use of parameterized constructor and default constructor. 89 #include<iostream.h> #include<conio.h> class integer { int m,n; public: integer() //Default constructor { m=0; n=0; } integer(int x,int y) //Parameterized constructor { m=x; n=y; } void display(void) { cout<<"value1="<<m<<endl;
  • 90. 90 cout<<"value2="<<n<<endl; } }; int main() { integer o1,o2(10,20); cout<<"Default constructor:"<<endl; o1.display(); cout<<"Parameterized constructor:"<<endl; o2.display(); getch(); } Output:
  • 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;
  • 92. 92 { alpha a1,a2,a3,a4; { cout<<"enter block1:"<<endl; alpha a5; } { cout<<"enter block2:"<<endl; alpha a6; } cout<<"re-enter in main:"<<endl; } getch(); }
  • 94. 38. WAP to illustrate the use of copy constructor. 94 #include<iostream.h> #include<conio.h> class code { int id; public: code() { } code(int a) { id=a; } code(code &i) { id=i.id; } void display(void) { cout<<"id="<<id<<endl;
  • 95. 95 } }; int main() { code a(100); code b=a; code c(a); code d; d=a; a.display(); b.display(); c.display(); d.display(); getch(); }
  • 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(); }
  • 99. 40. WAP to illustrate the use of unary operator overloading. 99 #include<iostream.h> #include<conio.h> class single { int a,b; public: single() { a=0; b=0; } single(int x,int y) { a=x; b=y; } void display() { cout<<"A="<<a<<endl;
  • 100. 100 cout<<"B="<<b<<endl; } single operator-() { a=-a; b=-b; } }; int main() { single o1; single o2(10,20); -o1; -o2; o1.display(); o2.display(); getch(); }
  • 102. 41. WAP to illustrate the use of binary operator overloading. 102 #include<iostream.h> #include<conio.h> class abc { int a,b; public: abc() { a=0; b=0; } abc(int x,int y) { a=x; b=y; } void display() { cout<<"a="<<a; cout<<"tb="<<b<<endl;
  • 103. 103 } abc operator +(abc &n) { abc temp; temp.a=a+n.a; temp.b=b+n.b; return(temp); } abc operator *(abc &n) { abc temp; temp.a= a * n.a; temp.b= b * n.b; return(temp); } abc operator /(abc &n) { abc temp; temp.a= a / n.a; temp.b= b / n.b; return(temp);
  • 104. 104 } }; int main() { abc a1(30,40); abc a2(10,20); abc a3,a4,a5; a3=a1+a2; a4=a1*a2; a5=a1/a2; a1.display(); a2.display(); cout<<"ADDTION"<<endl; a3.display(); cout<<"SUBTRACTION"<<endl; a4.display(); cout<<"DEVISION"<<endl; a5.display(); 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; }
  • 124. 51. WAP to copy contents of one file to another file. 124 #include<fstream.h> int main() { ifstream fin; fin.open("out.txt"); ofstream fout; fout.open("sample.txt"); char ch; while(!fin.eof()) { fin.get(ch); fout<<ch; } fin.close(); return 0; }