C++ Programming File
C++ Programming File
#include <iostream>
Output:
1
2. Write a C++ program to find the average of two numbers.
#include <iostream>
int main()
float num1 ,num2, sum, avg; cout<<"Enter the first number : \n"; cin>> num1;
sum=num1+num2; avg=sum/2;
Output:
2
3. Write a program to display the following output using a single cout statement.
Maths = 90
Physics = 77
Chemistry = 69
#include <iostream>
int main()
cout << "Maths=" << 90 << " Physics=" << 77 << " Chemistry=" << 69 << endl;
Output:
3
4. Write a program to read two numbers from keyboard and display the larger value on
screen.
#include <iostream>
int main()
else
cout << "Both numbers are equal: " << num1 << endl;
Output:
4
5. Write a program that inputs the characters from key board and display its
corresponding ASCII value on the screen.
#include <iostream>
int main()
char character;
cout << "The ASCII value of '" << character << "' is: " << int(character) << ;
Output:
5
6. Write a program to read the value of a, b and c and display the value of x, where
x=a/b-c.
int main() {
double a, b, c, x;
cin >> a;
cin >> b;
if (b != 0) {
x = a / b - c;
} else {
OUTPUT:
6
7. Write a program that will ask for a temperature in Fahrenheit and display it in
Celsius.
#include <iostream> using namespace std;
int main()
cout << fahrenheit << " Fahrenheit into celsius = " << celsius << " Celsius" << endl;
}
OUTPUT:
7
8. Write a program of Scope resolution operator.
#include <iostream>
int m =10;
int main()
int m =20;
cout<<"K = "<<k<<"\n";
cout<<"m = "<<m<<"\n";
OUTPUT:
8
9. Write a program of use of manipulators.
#include<iostream>
#include<iomanip>
int main()
<<setw(10)<<"allowance"<<setw(10)<<allowance<<endl
<<setw(10)<<"total"<<setw(10)<<total<<endl;
return 0;
OUTPUT:
9
10. Write a program of Inline function.
#include<iostream>
return(x*y);
return(p/q);
int main()
return 0;
OUTPUT:
10
11. Write a program of Default arguments.
int main()
float amount;
printline();
amount=value(5000.00,5);
return(sum);
printf("\n");
OUTPUT:
11
12. Write a program of calculating factorial of number.
#include <iostream>
long factorial(int n)
if (n == 0) return 1;
int main()
int num;
cout << "Factorial of " << num << " is: " <<factorial(num) << endl;
OUTPUT:
12
13. Write a program of solving tower of Hanoi problem.
#include <iostream>
if (d== 1)
cout << "\nShift top disk from tower " <<tower1 << " to tower " << tower2 <<
endl;
return;
cout << "\nShift top disk from tower " << tower1 << " to tower "<<tower2; towerOfHanoi(d - 1, tower3,
tower2, tower1);
}
int main()
int disk;
if(disk<1)
cout<<"\nThere are " << disk << " disk in tower 1\n"; towerOfHanoi(disk,'1','2','3');
cout<<"\n\n"<< disk <<" disk in tower 1 are shifted to tower 2\n"; return 0;
}
OUTPUT:
13
14. Write a program of function overloading.
#include<iostream>
int area(int);
double area(double);
int main() {
cout << "Calling the area() function for computing the area of a square (side=5) - " << area(5) << "\n";
cout << "Calling the area() function for computing the area of a rectangle (length=5, breadth=10) - " <<
cout << "Calling the area() function for computing the area of a circle (radius=5.5) - "
14
}
OUTPUT:
15
15. Write a program to use of Math functions.
#include <iostream>
#include <iomanip>
#include <math.h>
int main() {
cout << "7 to the power of 6 = " << pow(7.0, 6.0) << "\n";
cout << "Square root of 10 = " << sqrt(10.00) << "\n"; return 0;
OUTPUT:
16
16. Write a C++ program with class implementation.
#include<iostream>
class item
};
number=a; cost=b;
}
int main()
item x;
item y;
OUTPUT:
17
17. Write a program of nesting of member function.
#include<iostream>
class set
void input(void);
void display(void);
int largest(void);
};
int main()
set A; A.input();
18
A.display();
OUTPUT:
19
18. Write a C++ program of processing shopping list.
#include<iostream>
class ITEMS
int itemCode[m];
float itemPrice[m];
int count;
public:
void CNT(void){count=0;}
void getitem(void);
void displaySum(void);
void remove(void);
void displayItems(void);
};
cin>>itemCode[count];
cin>>itemPrice[count];
count++;
20
void ITEMS :: displaySum(void)
float sum=0;
int a;
cout<<"\n"<<itemCode[i]; cout<<"\n"<<itemPrice[i];
}
cout<<"\n";
int main()
do
cout<<"\n2:Display total value"; cout<<"\n3: Delete all item"; cout<<"\n4:Display all item";
21
cout<<"\n5:Quit";
switch(x)
} while(x!=5);
OUTPUT:
22
23
19. Write a program which declare static class member.
#include<iostream>
class item
int number;
public:
void getdata(int a)
void getcount(void)
cout<<"count:"; cout<<count<<"\n";
};
item a, b, c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
24
c.getdata(300);
a.getcount();
b.getcount();
c.getcount();
OUTPUT:
25
20. Write a program of static member function.
#include<iostream>
class test
int code;
public:
void setcode(void)
code =++count;
void showcode(void)
};
26
test t1,t2;
t1.setcode();
t2.setcode();
test :: showcount();
test :: showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
OUTPUT:
27
21. Write a program which declare the array of object.
#include<iostream>
class employee
char name[30];
float age;
public:
void getdata(void);
void putdata(void);
};
cout<<"Enter Name:";
cin>>name;
cout<<"Enter age";
cin>>age;
cout<<"Name:"<<name<<"\n";
cout<<"Age:"<<age<<"\n";
28
employee manager[size];
for(int i=0;i<size;i++)
cout<<"\nDetails of manager"<<i+1<<"\n";
manager[i].getdata();
cout<<"\n";
for(int i=0;i<size;i++)
cout<<"\nManager"<<i+1<<"\n";
manager[i].putdata();
OUTPUT:
29
22. Write a program which declared objects as argument.
#include<iostream>
class time
int hours;
int minutes;
public:
{hours=h;minutes=m;}
void puttime(void)
cout<<hours<<"hours and";
cout<<minutes<<"minutes"<<"\n";
void sum(time,time);
};
minutes=t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
30
hours=hours+t1.hours+t2.hours;
int main()
time T1,T2,T3;
T1.gettime(2,45);
T2.gettime(3,30);
T3.sum(T1,T2);
cout<<"T1=";T1.puttime();
cout<<"T2=";T2.puttime();
cout<<"T3=";T3.puttime();
}
OUTPUT:
31
23.Write a C++ Programming of friend function.
#include<iostream>
class sample
};
float mean(sample s)
return float(s.a+s.b)/2.0;
int main()
sample X; X.setvalue();
OUTPUT:
32
24. Write a program where a function friendly to two classes.
#include<iostream>
class ABC;
class XYZ
int x;
public:
};
class ABC
int a;
public:
};
33
{
if(m.x>=n.a)
cout<<m.x;
else
cout<<n.a;
int main()
ABC abc;
abc.setvalue(10);
XYZ xyz;
xyz.setvalue(20);
max(xyz,abc);
OUTPUT:
34
25.Write a program which swapping private data of classes.
#include<iostream>
class class_2;
class class_1
int value1;
public:
void display(void){cout<<value1<<"\n";}
};
class class_2
int value2;
public:
void display(void){cout<<value2<<"\n";}
};
35
int temp=x.value1;
x.value1=y.value2;
y.value2=temp;
int main()
class_1 C1;
class_2 C2;
C1.indata(100);
C2.indata(200);
C1.display();
C2.display();
exchange(C1,C2);
C1.display();
C2.display();
OUTPUT:
36
26.Write a program of returning objects.
#include<iostream>
class complex
float x;
float y;
public:
{x=real;y=imag;}
void show(complex);
};
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return(c3);
cout<<c.x<<"+j"<<c.y<<"\n";
37
int main()
complex A,B,C;
A.input(3.1,5.65);
B.input(2.75,1.2);
C=sum(A,B);
cout<<"A=";A.show(A);
cout<<"B=";B.show(B);
cout<<"C=";C.show(C);
OUTPUT:
38
27.Write a program of dereferencing operation.
#include<iostream>
class M
int x;
int y;
public:
x=a;
y=b;
};
int sum(M m)
int M ::*px=&M :: x;
int M ::*py=&M :: y;
M *pm=&m;
return S;
39
int main()
M n;
(n.*pf)(10,20);
cout<<"SUM="<<sum(n)<<"\n";
M *op=&n;
(op->*pf)(30,40);
cout<<"SUM="<<sum(n)<<"\n";
OUTPUT:
40