INDEX (C++) 1. Write a program to find a factorial of a given number. 2. Write a program to get roots of quadratic equation. 3. Write a program to calculate compound interest using Inline function. 4. Write a program to find out mean value using Friend function. 5. Write a program to find greatest of 3 no. using class& constructor. 6. Write a program to add 2 complex no. with the help of constructor. 7. Overload the area function to find area of triangle & circle. 8. Overload the following function to find volume of cube, cylinder and rectangle.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
INDEX(C++)
1. Write a program to find a factorial of a given number.
2. Write a program to get roots of quadratic equation.
3. Write a program to calculate compound interest using
Inline function.
4. Write a program to find out mean value using Friend function.
5. Write a program to find greatest of 3 no. using class& constructor.
6. Write a program to add 2 complex no. with the help of constructor.
7. Overload the area function to find area of triangle & circle.
8. Overload the following function to find volume of cube, cylinder
and rectangle.
9. Write a program to overload binary addition operator.
10. Write a program to overload unary prefix and postfix operator.
11. Write a program to find even and odd number and check whether
the no. is positive or negative
12. Write a program to swap values of 2 variables by passing pointer.
1.Write a program to find a factorial of a given number.
Program:-
#include<iiostream.h>
# include<conio.h>
void main( )
{
int f=1, n;
clrscr( );
cout<<“Enter a number”;
cin (n>0))
{
f=f*n;
n--;
}
cout<<“factorial is =%d”<<f;
getch( );
}
2. Write a program to get roots of quadratic equation. Program:-
#include<iiostream.h>
# include<conio.h>
void main( )
{
float a,b,c,d;
float r1,r2,r3;
clrscr( );
cout <<“Enter coefficient”;
cin>>a>>b>>c;
d=b*b-4*a*c;
if (d==0)
{
cout <<“r1=r1=”<<(-b)/2*a)<<endl;
}
else if (d>0)
{
cout<<“roots are equal”;
r1 =sqrt(d);
r2=(-b+r1)/(2*a);
r3=(-b-r1)/(2*a);
cout<< “r2=” “<<r2<<”endl;
cout<< “r3=” “<<r3<<”endl;
}
else
{
cout<< “roots are complex\n”;
r2=(-b)/(2*a);
r3=sqrt(-d)/(2*a);
cout<< “real part=”<<r2<<endl;
cout<< “imaginary part=”<<r3;
cout<<endl;
getch();
}
3.Write a program to calculate compound interest using Inline function.
Program:-
#include<math.h>
inline float intersect ()
{
float ci,r;
int t, p;
clrscr ()
cout<< “Enter principle, rate and time”;
cin>>p>>r>>t;
ci=p*(power(ci+r/100),t);
return(ci);
}
void main()
{
floatc;
c=intersect();
cout<< “ compound interest=”<<c<<endl;
getch();
}
4.Write a program for input two no and check they are equal or not.
# inc lude< ios t ream.h>
#include<coni.o. h>
vo id ina in ( )
{
in t a ,b ;
c l r sc r ( ) ;
cout«"Enter the Numbers";
ci n">b;
i f (a—b)
c< n t :< - "Number . in % oqun l " ;
el Se
c.oij t •' "Numbers are Not 1 " ;
ge tch ( ) ;
}
Output of program:
Enter the Numbers
12
11
Numbers a re Not equa l
5. Write a program for fibonacai series using function.
#include<iostream.h>
#include<conio.h>
void febo (int. n) ;
void mai n ()
{
int n;
clrscr ();
cout<<"Give how many numbers to print... "<<"\n";
cin»n;
cout<<"Series are as follow..."<<\n";
febo (n);
getch();
}
void febo(int n)
{
int sum,i,f=0,s=1;
cout<<f<<"/t";
for( i=2; i <n;1++)
{
sum-f+s;
f=s;
s=sum;
cout<<sum<<"/t";
}
Output of program:
Give how many number to print ...
series are as follow.......
6.Write a program for input two any no and find out squire using inline function
#include< iostream.h^
#include<conio.h>
inl ine f loat" square ( f loat x)/ / fact ion Definit ion {
f loat y;
y=x*x;
return(y);
}
void main()
{
f loat n,a;
cout<< x xEnter the value";
cin»n;
a=square (n);
cout«"The square is "<<a;
getch();
}
Output of program:
Enter the value
5.2
The square is:27.04
7.Write a program for input two or and display addition subtruction and multiplication of no using class.
#include< iostream.h^
#include<conio.h>
Class sample / /Class sample
{
int x,y, add, subs, mult i ; / / variable declare
Public : void setdata () / / function declare
{
Count<<"Enter the number x and y' ;
Com>>x>>Y; //end of funct ion
}
void sum ()
}
add=x+y;
count<< "The sumis:" <<add;
}
void sub ()
Subs=x-y;
count<<" The substruct ion is: " <<subs;
}
void mult i ()
mult i y=x*x;
cout<< "The multiplication is:"«multi;
} };
void main ()
{
sample s1; //s1 object of sample
clrscr();
s1.setdata (); //take a input
s1.sum() ; //set a result
s1.subs ();
s1.multi () ;
getch ();}
Output of program:
Enter the number x and y
25
18
The sum is: 43
Tho substruction is: 7
The multiplication is: 450
8.Write a program for display no of object created using static variable.
#include<iostream.h>
#include<conio.h>
class test //class deceleration
{
int code;
static int count; //static member veriable public:
void setdata(void)
{
code = ++count;
}
void showcount(void)
{
cout<<"Object: number: }"<<code<<"/n;
}
Static void showcoum (void)
//static member function
{
count<<""count: "«count<<"/n"";
}};
int test :: count;
int main()
{
test t1,t2;
t1.setdata();
t2.setdata();
test :: showcount ()
//accessing static function
test t3;
t3.setdata();
test :: showcount() ;n
t1.showcode();
t2 . showcode () ;
t3. showcode
() ;
return ();
}
Output of program:
Count: 2
count: 3
Object number:1
Object number:2
Object number:3
9.Write a program to find greatest of 3 no. using class& constructor.
Program:-
#include<iiostream.h>
# include<conio.h>
Class greatest
{
Public:
Int a,b,c;
Public:
Greatest(intx,inty,intz)
{
A=x;
B=y;
C=z;
}
Void cal data()
{
If (a>b & & a>c)
{
cout <<a<< “is greater”;
}
else if(b>c)
{
cout<<b<< “is greater”;
}
else
{
cout<<c<< “is greater”;
}
}
};
void main()
{
greatest g(10,20,25);
g caldata();
getch();
}
10.Write a program to add 2 complex no. with the help Class and constructor.
Program:-
#include<iiostream.h>
# include<conio.h>
class complex
{
float x,y;
public:
complex(float a, float b0
{
x=a;
y=b;
}
complex()
}
{
friend complex sum (complex , complex);
friend void show (complex);
~ complex()
}
}
};
complex sum(complex c1, complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return(c3);
}
void show (complex c)
{
cout <<c.x<< “+i+”,<<c.y<< endl;
}
void main()
{
clrscr();
complex A(2.7,3.5) ;
complex B(1.6 );
complex c;
c=sum(A,B);
cout<< “A=”;
show (A);
cout<< “B=”;
show (B);
cout<< “c=”;
show(c);
}
getch();
}
11. Write a program to find out mean value using Friend function.
Program:-
#include<iiostream.h>
# include<conio.h>
Class sample
{
Int a;
Int b;
Public:
Void (set value() )
{
A=25;
B=40;
}
Friend float mean (sample s);
};
Float mean (sample s)
{
Return float (s.a+s.b/2.0);
}
Int main()
{
Int main(
{
Sample x;
x.set value();
cout << “mean value =”<<mean(x)<< “\n”;
return();
getch();
}
12. Overload the area function to find area of triangle & circle.
Program :-
#include<iiostream.h>
# include<conio.h>
float area (int);
float area (float, int);
int main(){clrscr();
cout<<area(8)<< “\n”;
cout<<area (3.5,3)<< “\n”;
return 0;
}
float area (int r)
{
return (3.14*r*r);
}
float area (float b, int b)
{
return(.5*b*h);
}
getch();
}
13.Overload the following function to find volume of cube, cylinder and rectangle.