C++ Programs

 1.Write a program to enter mark of 6 different subjects and find out the total marks(using cin and cout statement)

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int sub1,sub2,sub3,sub4,sub5,sub6,total;

cout<<"enter marks of sub1";

cin>>sub1;

cout<<"enter marks of sub2 ";

cin>>sub2;

cout<<"enter marks of sub3";

cin>>sub3;

cout<<"enter marks of sub4";

cin>>sub4;

cout<<"enter marks of sub5";

cin>>sub5;

cout<<"enter marks of sub6";

cin>>sub6;

total=sub1+sub2+sub3+sub4+sub5+sub6;

cout<<"total marks is:"<<total;

getch();

}

2.Write a Program to Find Factorial of a number.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int i,num,fact;

fact=1;

cout<<"/n Enter a Number:";

cin>>num;

for(i=1;i<=num;i++)

fact=fact*i;

cout<<"factorial of "<<num<<"is"<<fact;

getch();

}

3.Write a Program to find Largest of three numbers.(using Class)

#include<iostream.h>

#include<conio.h>

class largest

{

public:

int a ,b,c;

void input()

{

cout<<"enter three marks";

cin>>a>>b>>c;

};

void show()

{

if(a>b)

{

if(a>c)

cout<<"a is greater:"<<a;

else

cout<<"c is greter:"<<c;

}

else

{

if(b>c)

cout<<"b is greter:"<<b;

else

cout<<"c is greater:"<<c;

}

}

};

void main()

{

clrscr();

  largest obj;

  obj.input();

  obj.show();

  getch();

  } 


4.WAP to find Greatest Of Three Numbers Using Function.

#include<iostream.h>

#include<conio.h>

int largest(int,int,int);

void main()

{

clrscr();

int n1,n2,n3,show;

cout<<"enter three num";

cin>>n1>>n2>>n3;

show=largest(n1,n2,n3);

cout<<"largest is"<<show;

getch();


}

int largest(int n1,int n2,int n3)

{

if((n1>n2)&&(n1>n3))

return n1;

else if((n2>n3)&&(n2>n1))

return n2;

else

return n3;

return 0;

}

5. Program for INLINE Function

#include<iostream.h>

#include<conio.h>

inline float mul(float x,float y)

{

   return(x*y);

}

inline float div(float p,float q)

{

return(p/q);

}

 void main()

{

clrscr();

float a=12.345;

float b=9.82;

cout<<mul(a,b)<<endl;

cout<<div(a,b)<<endl;

getch();

}

6.Write a program for overloading of Unary -- operator.

#include<iostream.h>

#include<conio.h>

class abc

{

public:

int a;

 void bc()

{

a=5;

}

void operator --()

{

a--;

}

void show()

{

cout<<a;

}

};

void main()

{

clrscr();

abc ob;

ob.bc();

ob--;

ob.show();

getch();

}

7.Write a program for overloading of Binary + operator. 

#include<iostream.h>

#include<conio.h>

class abc

{

int a,b;

public:

abc(int x,int y)

{

a=x;

b=y;

}

void show()

{

cout<<"a is"<<a<<"   "<<"b is"<<b<<endl;

}

abc operator +(abc obj)

{

abc temp(0,0);

temp.a=a+obj.a;

temp.b=b+obj.b;

return temp;

}

};

void main()

{

clrscr();

abc ob(10,20),ob1(20,40),ob2(0,0);

ob2=ob+ob1;

ob2.show();

getch();

}









Post a Comment

0 Comments