Programs

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

While loop:

#include <iostream>
using namespace std;
int main()
{
int x=2;
while(x>0)
{
x--;
cout<<x<<endl;
}
cout<<"Done."<<endl;
return 0;
}

Mark averaging using while loop:


#include <iostream>
using namespace std;
int main()
{
float nextmark, sum=0;
int count=0;
cin>> nextmark;
while(nextmark>=0)
{
sum=sum+nextmark;
count=count+1;
cin>> nextmark;
}
cout<<sum/count<<endl;
return 0;
}

Euclid’s algorithm for computation of GCD:

#include <iostream>
using namespace std;
int main()
{
{
int m,n;
cin>>m>>n;
while(m%n!=0){
int nextm=n;
int nextn=m%n;
m=nextm;
n=nextn;
}
cout<<n<<endl;
}
return 0;
}

Income tax calculation using if-else:

#include <iostream>
using namespace std;

int main()
{
float tax,income;
cin>>income;
if (income<=180000)
tax=0;
if (income<=500000)
tax=(income-180000)*0.1;
if (income<=800000)
tax=(income-500000)*0.2+32000;
else
tax=(income-800000)*0.3+92000;
cout<<tax<<endl;

return 0;
}

Bool data type:;

#include <iostream>
using namespace std;

int main()
{
{
int n, divisor=2; cin>>n;
bool divisorfound=false; //no divisor for n found so for.

{
if(n%divisor==0)

divisorfound=true;
cout<<divisorfound;
divisor=divisor+1;
}
if(!divisorfound)
cout<<"Prime.\n";
else cout<<"Composite.\n";
}
Functions GCD:
#include <iostream>
using namespace std;
int main()
{
int gcd(int m, int n)
{
while(m%n!=0){
int r= m%n;
m=n;
n=r;
}
return n;
}
int a=36, b=24;
cout << gcd(a,b);
cout << gcd(99,47);
}
return 0;
}
return 0;
}

Functions-Passing arguments by values

#include <iostream>
using namespace std;
int sum(int a, int b=10, int c=15, int d=20)
{
return(a+b+c+d);
}
int main()
{
int a=2, b=3,c=4, d=5;
cout<<"sum="<<sum(a,b,c,d)<<endl;
cout<<"sum="<<sum(a,b,c)<<endl;
cout<<"sum="<<sum(a,b)<<endl;
cout<<"sum="<<sum(a)<<endl;
return 0;
}

Functions- Passing values by reference (exchanging of numbers)

#include <iostream>
using namespace std;
int exchange(int &m, int &n)
{
int temp=m;
m=n;
n=temp;
cout<<m<<endl<<&m<<endl;
cout<<n<<endl<<&n<<endl;
return n;
}
using namespace std;
int main ()
{
{
int a=3,b=4;
cout<<"main program"<<endl<<&a<<","<<&b<<endl;
exchange (a,b);
cout<<"mainprogram"<<endl<<a<<endl<<","<<b<<endl;
}

Function recursion

#include <iostream>
using namespace std;
int gcd(int m, int n)
{ cout<<"Executing function"<<endl;
int result;
if(m%n==0)
result=n;
else
result=gcd(n,m%n);
return result;
}
int main()
{
int result=gcd(205,123);
cout<<"result seen in main program"<<result<<endl;
}

Pointers basic:

#include <iostream>
using namespace std;
int main()
{
int *v,*q,p=1;
v=&p;
q=v;
cout<<*v<<endl<<*q<<endl;

v=&p;
cout<<v<<endl<<&p<<endl<<p<<endl;
return 0;
}

Inline function and macros:

#include <iostream>
#define SQUARE(v) v*v
using namespace std;
inline float square(float j)
{
return(j*j);
}
void ctop(double x, double y, double *pr, double *ptheta)
{*pr=SQUARE(++y);
*ptheta=square(++x);
return;
}
int main()
{
double r,theta,macroc,inlinec;
ctop(2,2,&r,&theta);
cout<<"macroc="<<r<<endl<<"inlinec="<<theta<<endl;
}

Functions – pass by address


Cartesian to polar conversion

#include<iostream>
#include<cmath>
using namespace std;
void ctop (double x, double y, double *pr, double *ptheta)
{
*pr=sqrt(x*x+y*y);
*ptheta=atan2(y, x); //arc tan
cout<<&pr<<endl<<pr<<endl<<*pr<<endl;
return;
}
int main()
{
double r, theta;
cout<<&r<<endl;
ctop(1,1,&r,&theta);
cout<<r<<endl<<theta <<endl;
}

Namespace:

#include <iostream>
int main()
{
int x=2;
while(x>0)
{
x--;
std:: cout<<x<<std::endl;
}
std::cout<<"Done."<<std::endl;
return 0;
}

Bisection - to find square of any value

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double xl=0,xr=2, xm, epsilon=0.00001;
while (xr-xl>=epsilon)
{
xm=(xl+xr)/2;
if((xl*xl-2> 0 && xm*xm-2>0)||(xl*xl-2< 0 && xm*xm-2<0))
xl=xm;
else xr=xm;
cout<<xl<<','<<xr<<endl;
}
cout<<"our final answer"<<xl<<endl;
cout<<"library function"<<sqrt(2)<<endl;
}

Functions-Passing function to function

#include <iostream>
#include <functional>
using namespace std;
double bisection (double xl, double xr, double epsilon, function<double(double)>h)
{ bool xlis=(h(xl)>0);
while(xr-xl>=epsilon){
double xm=(xl+xr)/2;
bool xmis=(h(xm)>0);
if(xlis==xmis) xl=xm;
else
xr=xm;
}
return xl;
}
double f(double x) {return x*x-2; }
double g(double x){return x*x*x-3; }
int main()
{
cout<<bisection(1,2,0.0001,f)<<endl;
cout<<bisection(1,3,0.0001,g)<<endl;
}

Default arguments:

#include <iostream>
using namespace std;
int sum(int a, int b=10, int c=15, int d=20)
{
return(a+b+c+d);
}
int main()
{
int a=2, b=3,c=4, d=5;
cout<<"sum="<<sum(a,b,c,d)<<endl;
cout<<"sum="<<sum(a,b,c)<<endl;
cout<<"sum="<<sum(a,b)<<endl;
cout<<"sum="<<sum(a)<<endl;
return 0;
}

Function overloading:

#include <iostream>
using namespace std;
# define pi 3.1428
int calcarea(int length, int breadth)
{
return (length * breadth);
}
float calcarea (double base, double height)
{
return((0.5)*base*height);
}
float calcarea(double radius)
{
return((4/3)*pi*radius*radius*radius);
}
int main()
{
int area1;
float area2;
double area3;
area1= calcarea(10,20);
area2=calcarea(4.5,2.1);
area3=calcarea(3.12145);
cout<<"area of rectangle is"<<area1<<endl;
cout<<"areaof triangle is"<<area2<<endl;
cout<<"area of sphere is "<<area3<<endl;
}

You might also like