0% found this document useful (0 votes)
0 views

C++ Programming File

The document contains a series of C++ programming exercises, each demonstrating different programming concepts such as basic input/output, functions, classes, and control structures. Each exercise includes code snippets and expected outputs, covering topics like calculating averages, using manipulators, function overloading, and implementing classes with member functions. The exercises serve as practical examples for learning and understanding C++ programming.

Uploaded by

kayitis468
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

C++ Programming File

The document contains a series of C++ programming exercises, each demonstrating different programming concepts such as basic input/output, functions, classes, and control structures. Each exercise includes code snippets and expected outputs, covering topics like calculating averages, using manipulators, function overloading, and implementing classes with member functions. The exercises serve as practical examples for learning and understanding C++ programming.

Uploaded by

kayitis468
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

1. Write a simple C++ code.

#include <iostream>

using namespace std; int main()

cout << " C++ is better than C .\n ";

Output:

1
2. Write a C++ program to find the average of two numbers.

#include <iostream>

using namespace std;

int main()

float num1 ,num2, sum, avg; cout<<"Enter the first number : \n"; cin>> num1;

cout<<"Enter the second number : \n"; cin>> num2;

sum=num1+num2; avg=sum/2;

cout<<"Sum : "<< sum <<"\n"; cout<<"Average : "<< avg <<"\n";

Output:

2
3. Write a program to display the following output using a single cout statement.

Maths = 90

Physics = 77

Chemistry = 69

#include <iostream>

using namespace std;

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()

int num1, num2;

cout << "Enter first number: ";

cin >> num1;

cout << "Enter second number: ";

cin >> num2;

if (num1 > num2)

cout << "Larger number: " << num1 << endl;

else if (num2 > num1)

cout << "Larger number: " << num2 << endl;

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>

using namespace std;

int main()

char character;

cout << "Enter a character: ";

cin >> 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.

#include <iostream> using namespace std;

int main() {

double a, b, c, x;

cout << "Enter the value of a: ";

cin >> a;

cout << "Enter the value of b: ";

cin >> b;

cout << "Enter the value of c: "; cin >> c;

if (b != 0) {

x = a / b - c;

cout << "The value of x is: " << x << endl;

} else {

cout << "Error! Division by zero is not allowed." << endl;

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()

double fahrenheit, celsius;

cout << "Enter temperature in Fahrenheit: ";

cin >> fahrenheit;

celsius = (fahrenheit - 32) * 5.0/9.0;

cout << fahrenheit << " Fahrenheit into celsius = " << celsius << " Celsius" << endl;

}
OUTPUT:

7
8. Write a program of Scope resolution operator.

#include <iostream>

using namespace std;

int m =10;

int main()

int m =20;

int k=m; int m=30;

cout<<"We are in inner block \n";

cout<<"K = "<<k<<"\n";

cout<<"m = "<<m<<"\n";

cout<<":: m = "<< :: m<< "\n";

cout<<"\nWe are in outer block\n"; cout<<"m = "<<m<<"\n";

cout<<":: m = "<< :: m<<"\n";

OUTPUT:

8
9. Write a program of use of manipulators.

#include<iostream>

#include<iomanip>

using namespace std;

int main()

int basic= 950,allowance=95,total=1045;

cout << setw(10)<<"basic"<<setw(10)<<basic<<endl

<<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>

using namespace std;

inline float mul(float x,float y)

return(x*y);

inline double div(double p,double q)

return(p/q);

int main()

float a=12.345; float b=9.82;

cout<< mul(a,b)<<"\n"; cout<< div(a,b)<<"\n";

return 0;

OUTPUT:

10
11. Write a program of Default arguments.

#include<iostream> using namespace std;

int main()

float amount;

float value(float p,int n,float r=0.15);

void printline(char ch='*', int len=40);

printline();

amount=value(5000.00,5);

cout<<"\n Final value=" <<amount <<"\n\n"; printline('=');

float value(float p, int n, float r)

int year=1; float sum=p; while(year<=n)


{

sum= sum*(1+r); year=year+1;


}

return(sum);

void printline(char ch,int len)

for(int i=1;i<=len;i++) printf("%c",ch);

printf("\n");

OUTPUT:

11
12. Write a program of calculating factorial of number.

#include <iostream>

using namespace std;

long factorial(int n)

if (n == 0) return 1;

return (n * factorial(n - 1));

int main()

int num;

cout << "Enter a positive integer : "; cin >> num;

cout << "Factorial of " << num << " is: " <<factorial(num) << endl;

OUTPUT:

12
13. Write a program of solving tower of Hanoi problem.

#include <iostream>

using namespace std;

void towerOfHanoi(int d,char tower1, char tower2, char tower3) {

if (d== 1)

cout << "\nShift top disk from tower " <<tower1 << " to tower " << tower2 <<

endl;

return;

towerOfHanoi(d - 1,tower1, tower3, tower2 );

cout << "\nShift top disk from tower " << tower1 << " to tower "<<tower2; towerOfHanoi(d - 1, tower3,

tower2, tower1);
}

int main()

int disk;

cout<< "\nEnter the number of disks: "; cin>> disk;

if(disk<1)

cout<<"\nThere are no disk of shift\n"; else

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>

using namespace std;

int area(int);

int area(int, 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) - " <<

area(5, 10) << "\n";

cout << "Calling the area() function for computing the area of a circle (radius=5.5) - "

<< area(5.5) << "\n"; return 0;

int area(int side) {

return (side * side);

int area(int length, int breadth)

return (length * breadth);

14
}

double area(double radius)

return (3.14 * radius * radius);

OUTPUT:

15
15. Write a program to use of Math functions.

#include <iostream>

#include <iomanip>

#include <math.h>

using namespace std;

int main() {

cout << fixed << setprecision(2);

cout << "sin(100) = " << sin(100.00) << "\n";

cout << "cos(100) = " << cos(100.00) << "\n";

cout << "tan(100) = " << tan(100.00) << "\n";

cout << "7 to the power of 6 = " << pow(7.0, 6.0) << "\n";

cout << "log10(10) = " << log10(10.00) << "\n";

cout << "Square root of 10 = " << sqrt(10.00) << "\n"; return 0;

OUTPUT:

16
16. Write a C++ program with class implementation.

#include<iostream>

using namespace std;

class item

int number; float cost; public:

void getdata(int a,float b); void putdata(void)

cout<<"number :"<<number<<"\n"; cout<<"cost:"<<cost<<"\n";

};

void item::getdata(int a,float b)

number=a; cost=b;
}

int main()

item x;

cout<<"\n object x" <<"\n"; x.getdata(100,299.95); x.putdata();

item y;

cout<<"\n object y" <<"\n"; y.getdata(200,175.50); y.putdata();

OUTPUT:

17
17. Write a program of nesting of member function.

#include<iostream>

using namespace std;

class set

int m,n; public:

void input(void);

void display(void);

int largest(void);

};

int set :: largest(void)

if(m>=n) return(m); else return(n);


}

void set :: input(void)

cout<<"Input values of m and n"<<"\n"; cin>>m>>n;


}

void set :: display(void)

cout<<"Largest value ="<<largest()<<"\n";

int main()

set A; A.input();

18
A.display();

OUTPUT:

19
18. Write a C++ program of processing shopping list.

#include<iostream>

using namespace std;

const int m=50;

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);

};

void ITEMS :: getitem(void)

cout<<"Enter item code:";

cin>>itemCode[count];

cout<<"Enter item cost:";

cin>>itemPrice[count];

count++;

20
void ITEMS :: displaySum(void)

float sum=0;

for(int i=0;i<count;i++) sum=sum+itemPrice[i];

cout<<"\n Total value:"<<sum<<"\n";

void ITEMS :: remove(void)

int a;

cout<<"Enter item code:"; cin>>a;

for(int i=0;i<count;i++) if(itemCode[i]==a) itemPrice[i]=0;

void ITEMS :: displayItems(void)

cout<<"\n Code Price \n"; for(int i=0;i<count;i++)

cout<<"\n"<<itemCode[i]; cout<<"\n"<<itemPrice[i];
}

cout<<"\n";

int main()

ITEMS order; order.CNT(); int x;

do

cout<<"\n You can do the following:"<<"Enter appropriate number\n"; cout<<"\n1:Add an item";

cout<<"\n2:Display total value"; cout<<"\n3: Delete all item"; cout<<"\n4:Display all item";

21
cout<<"\n5:Quit";

cout<<"\n\n What is your option?"; cin>>x;

switch(x)

case 1: order.getitem(); break;

case 2: order.displaySum(); break;

case 3: order.remove(); break;

case 4: order.displayItems(); break; default: cout<<"Error in input; try again\n";


}

} while(x!=5);

OUTPUT:

22
23
19. Write a program which declare static class member.

#include<iostream>

using namespace std;

class item

static int count;

int number;

public:

void getdata(int a)

number =a; count ++;


}

void getcount(void)

cout<<"count:"; cout<<count<<"\n";

};

int item :: count; int main()

item a, b, c;

a.getcount();

b.getcount();

c.getcount();

a.getdata(100);

b.getdata(200);

24
c.getdata(300);

cout<<"after reading data"<<"\n";

a.getcount();

b.getcount();

c.getcount();

OUTPUT:

25
20. Write a program of static member function.

#include<iostream>

using namespace std;

class test

int code;

static int count;

public:

void setcode(void)

code =++count;

void showcode(void)

cout << "object number:"<< code <<"\n";

static void showcount(void)

cout << "count:" << count << "\n";

};

int test :: count; int main()

26
test t1,t2;

t1.setcode();

t2.setcode();

test :: showcount();

test t3; t3.setcode();

test :: showcount();

t1.showcode();

t2.showcode();

t3.showcode();

return 0;

OUTPUT:

27
21. Write a program which declare the array of object.

#include<iostream>

using namespace std;

class employee

char name[30];

float age;

public:

void getdata(void);

void putdata(void);

};

void employee :: getdata(void)

cout<<"Enter Name:";

cin>>name;

cout<<"Enter age";

cin>>age;

void employee :: putdata(void)

cout<<"Name:"<<name<<"\n";

cout<<"Age:"<<age<<"\n";

const int size=3;

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>

using namespace std;

class time

int hours;

int minutes;

public:

void gettime(int h,int m)

{hours=h;minutes=m;}

void puttime(void)

cout<<hours<<"hours and";

cout<<minutes<<"minutes"<<"\n";

void sum(time,time);

};

void time :: sum(time t1,time t2)

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>

using namespace std;

class sample

int a; int b; public:

void setvalue() {a=25;b=40;}

friend float mean(sample s);

};

float mean(sample s)

return float(s.a+s.b)/2.0;

int main()

sample X; X.setvalue();

cout<<"Mean value ="<<mean(X)<<"\n";

OUTPUT:

32
24. Write a program where a function friendly to two classes.

#include<iostream>

using namespace std;

class ABC;

class XYZ

int x;

public:

void setvalue(int i) {x=i;}

friend void max(XYZ,ABC);

};

class ABC

int a;

public:

void setvalue(int i) {a=i;}

friend void max(XYZ,ABC);

};

void max(XYZ m,ABC n)

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>

using namespace std;

class class_2;

class class_1

int value1;

public:

void indata(int a) {value1=a;}

void display(void){cout<<value1<<"\n";}

friend void exchange(class_1 &,class_2 &);

};

class class_2

int value2;

public:

void indata(int a){value2=a;}

void display(void){cout<<value2<<"\n";}

friend void exchange(class_1 &,class_2 &);

};

void exchange(class_1 & x,class_2 & y)

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);

cout<<"Values before exchange"<<"\n";

C1.display();

C2.display();

exchange(C1,C2);

cout<<"Values after exchange"<<"\n";

C1.display();

C2.display();

OUTPUT:

36
26.Write a program of returning objects.

#include<iostream>

using namespace std;

class complex

float x;

float y;

public:

void input(float real,float imag)

{x=real;y=imag;}

friend complex sum(complex,complex);

void show(complex);

};

complex sum(complex c1,complex c2)

complex c3;

c3.x=c1.x+c2.x;

c3.y=c1.y+c2.y;

return(c3);

void complex :: show(complex c)

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>

using namespace std;

class M

int x;

int y;

public:

void set_xy(int a,int b)

x=a;

y=b;

friend int sum(M m);

};

int sum(M m)

int M ::*px=&M :: x;

int M ::*py=&M :: y;

M *pm=&m;

int S=m.*px+ pm->*py;

return S;

39
int main()

M n;

void(M :: *pf)(int, int)=&M :: set_xy;

(n.*pf)(10,20);

cout<<"SUM="<<sum(n)<<"\n";

M *op=&n;

(op->*pf)(30,40);

cout<<"SUM="<<sum(n)<<"\n";

OUTPUT:

40

You might also like