0% found this document useful (0 votes)
435 views25 pages

Index (C++)

The document contains 12 C++ programs with their source code. The programs cover topics such as finding the factorial of a number, solving quadratic equations, calculating compound interest, overloading operators, using classes and constructors, friend functions and more.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
435 views25 pages

Index (C++)

The document contains 12 C++ programs with their source code. The programs cover topics such as finding the factorial of a number, solving quadratic equations, calculating compound interest, overloading operators, using classes and constructors, friend functions and more.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 25

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. #include<iostream.h> #include<coni.o. h> void inain() { int a,b; clrscr(); cout"Enter t h e Numbers"; ci n">b; if (ab) c< nt:<- "Number.in% oqunl"; el Se c.oij t ' "Numbers are Not 1 " ; getch (); } Output of program: Enter t h e Numbers 12 11 Numbers a r e Not e q u a 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"; cinn; 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> inline float" square (float x)//faction Definition { float y; y=x*x; return(y); } void main() { float n,a; cout<< x x Enter the value"; cinn; 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 { int x,y, add, subs, multi; // variable declare Public : void setdata () { Count<<"Enter the number x and y'; Com>>x>>Y; } void sum () } add=x+y; count<< "The sumis:" <<add; } void sub () Subs=x-y; //end of function //function declare //Class sample

count<<" The substruction is: " <<subs; } void multi () multi y=x*x; cout<< "The multiplication is:"multi; } }; void main () { sample s1; clrscr(); s1.setdata (); s1.sum() ; 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 //take a input //set a result //s1 object of sample

8.Write a program for display no of object created using static variable. #include<iostream.h> #include<conio.h> class test { int code; static int count; void setdata(void) { code = ++count; } void showcount(void) { cout<<"Object: number: }"<<code<<"/n; } Static void showcoum (void) //static member function { //static member veriable public: //class deceleration

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. Program: #include<iiostream.h> # include<conio.h> int volume (int); double volume( double,int) long volume (long, int,int); int main() { cout <<volume(0)<< \n; cout <<volume(2.5, 8)<< \n; cout<<volume(100l, 75, 15)<< \n; return 0; } int volume (int s) { return(s*s*s); }

double volume (double, inth) { return(3.14*r*r*h); } long volume(longl ,intb,inth) { return(l*b*h) } getch(); }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy