WAP to sort n numbers in descending order using selection sort.


#include<iostream>
#include<conio.h>

using namespace std;

void display(int b[]);

int main()
{
int a[10]={2,3,6,8,9,0,1,4,7,2},large,index;

cout<<"Numbers before SELECTION SORT\n";

display(a);

for(int i=9;i>0;i--)
{
large=a[9];
index=0;

for(int j=1;j<=i;j++)
{
if(a[j]>large)
{
large=a[j];
index=j;
}
a[index]=a[i];
a[i]=large;
}

}

cout<<"\nNumbers after SELECTION SORT\n";

display(a);

getch();
return(0);
}

void display(int b[])
{
for(int i=0;i<10;i++)
{
cout<<b[i]<<" ";
}
}

WAP to sort n numbers in ascending order using bubble sort.


#include<iostream>
#include<conio.h>

using namespace std;

void display(int b[]);

int main()
{
int a[]={9,5,3,7,4,6,1,2,8,9,4,5,6,2,0},n;

cout<<"Numbers before BUBBLE SORT :\n";

display(a);

cout<<"\nNumber after BUBBLE SORT : \n";

for(int i=0;i<14;i++)
{
for(int j=0;j<14-i;j++)
{
if(a[j]>a[j+1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}

    display(a);
    
getch();
return(0);
}

void display(int b[])
{
    for(int i=0;i<15;i++)
{
cout<<b[i]<<" ";
}

}

WAP to find the largest and smallest among n numbers


#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
int n;
cout<<"How many numbers : ";cin>>n;

int *p=new int(n);

cout<<"\nEnter the "<<n<<" numbers : \n";

for(int i=1;i<=n;i++)
{
cin>>*(p+i);
}

int smallest = *p, largest= *p;

for(int i=1;i<=n; i++)
{
if((smallest>*(p+i)))
{
smallest=*(p+i);
   
}
if(largest<*(p+i))
   largest=*(p+i);


}
cout<<"\nLargest number = "<<largest;
cout<<"\nSmallest number = "<<smallest;



getch();
return(0);
}

WAP to find GCD of two number using function.


#include<iostream>
#include<conio.h>

using namespace std;

int gcd(int x,int y);

int main()
{
int a,b;
cout<<"Enter two number : ";cin>>a>>b;
cout<<"\nThe GCD of "<<a<<" and "<<b<<" is "<<gcd(a,b);
getch();
return(0);
}

int gcd(int x,int y)
{
int z=x%y;
if(z==0)
return(y);
else
return(gcd(y,z));
}

WAP to reverse a number using function.


#include<iostream>
#include<conio.h>

using namespace std;

int rev(int n);

int main()
{
int x;
cout<<"Enter any number to reverse it : ";cin>>x;
cout<<"\nThe reverse form of "<<x<<" is "<<rev(x);
getch();
return(0);
}

int rev(int n)
{   int num;
while(n!=0)
{
int r=n%10;
num=num*10+r;
n=n/10;
}
return(num);

}

WAP to calculate power of base using recursion.


#include<iostream>
#include<conio.h>

using namespace std;

long int power (float b,int p);
int main()
{
float x;
int y;
cout<<"Enter the base value : ";cin>>x;
cout<<"\nEnter the power value : ";cin>>y;
cout<<"\n\nThe "<<x<<" to the power "<<y<<" is "<<power(x,y);
getch();
return(0);

}

long int power (float b,int p)
{
if(p==0)
return(1);
else
return(b*power(b,(p-1)));
}

WAP to round a value to nearest integer using floor function.


#include<iostream>
#include<conio.h>
#include<cmath>

using namespace std;

int main()
{
float n;
cout<<"Enter any floating point number : ";cin>>n;
cout<<"\nThe round value of "<<n<<" is "<<floor(n);
getch();
return(0);
}

WAP to swap to numeric data using function.(Reference variable).


#include<iostream>
#include<conio.h>

using namespace std;

void swap(int &,int &);

int main()
{
int a,b;
cout<<"Enter first number : ";cin>>a;
cout<<"Enter second number : ";cin>>b;
swap(a,b);
cout<<"After Swap !!!";
cout<<"\nFirst number is "<<a;
cout<<"\nSecond number is "<<b;
getch();
return(0);
}

void swap(int &x,int &y)
{
int z=x;
x=y;
y=z;
}

WAP to find cube of an integer using inline function.


#include<iostream>
#include<conio.h>

using namespace std;

int cube(int n)
{
return(n*n*n);
}

int main()
{
int n;
cout<<"Enter any number : ";cin>>n;
cout<<"Cube of "<<n<<" is "<<cube(n);
getch();
return(0);
}

WAP that uses function prime() for testing prime. Function should take integer argument and return Boolean value.


#include<iostream>
#include<conio.h>

using namespace std;

bool prime(int n);

int main()
{
int n;
cout<<"Enter any number : ";cin>>n;
if(prime(n)==1)
   cout<<n<<" is a prime number.";
else
   cout<<n<<" is not a prime number.";
getch();
return(0);
}

bool prime(int n)
{
if(n==0||n==1||n==2)
   return(0);
else
   {
for(int i=2;i<=(n/2);i++)
{
if(n%i==0)
   return(0);
}
return(1);
}
}

WAP to swap to character data using function.


#include<iostream>
#include<conio.h>

using namespace std;

void swap(char &,char &);

int main()
{
char ch1,ch2;
cout<<"Enter first character = ";cin>>ch1;
cout<<"Enter second character = ";cin>>ch2;
swap(ch1,ch2);
cout<<"After swapping character : ";
cout<<"\nNow first character = "<<ch1;
cout<<"\nNow second character = "<<ch2;
getch();
return(0);
}

void swap(char &a,char &b)
{
char temp;
temp=a;
a=b;
b=temp;
}

C++ : WAP to find surface area of box using inline function.


#include<iostream>
#include<conio.h>

using namespace std;

float surface_area(float l,float b,float h)
{
return(2*(l*b+b*h+l*h));
}

int main()
{
float l,b,h;
cout<<"Length = ";cin>>l;
cout<<"Breadth = ";cin>>b;
cout<<"Height = ";cin>>h;
cout<<"Surface Area = "<<surface_area(l,b,h);
getch();
return(0);
}

C++ : WAP to calculate simple interest using inline function.


#include<iostream>
#include<conio.h>

using namespace std;

float interest(float p,float t,float r)
{
return((p*t*r)/100);
}

int main()
{
float p,t,r;
cout<<"Principle = ";cin>>p;
cout<<"Time = ";cin>>t;
cout<<"Rate = ";cin>>r;
cout<<"Simple Interest = "<<interest(p,t,r);
getch();
return(0);

}

C++ : Write a program to print line of n character of user given character.


#include<iostream>
#include<conio.h>

using namespace std;

void display(char ch,int n);

int main()
{
int n;
char ch;
cout<<"Enter any character : ";cin>>ch;
cout<<"Enter the number of character '"<<ch<<"' to display : ";cin>>n;
display(ch,n);
getch();
return(0);
}

void display(char ch,int n)
{
for(int i=1;i<=n;i++)
{
cout<<ch;
}
}

C++ : Write a program using function to print line of 50 asterisk.


#include<iostream>
#include<conio.h>

using namespace std;

void display(char ch);

int main()
{
display('*');
getch();
return(0);
}

void display(char ch)
{
for(int i=1;i<=50;i++)
{
cout<<ch;
}
}

C : round robin algorithm implementation


#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
void main()
{
char p[10][5];
int et[10],wt[10],timer=3,count,pt[10],rt,i,j,totwt=0,t,n=5,found=0,m;
float avgwt;

for(i=0;i<n;i++)
{
printf("Enter the process name : ");
scanf("%s",&p[i]);
printf("Enter the processing time : ");
scanf("%d",&pt[i]);
}
m=n;
wt[0]=0;
i=0;
do
{
if(pt[i]>timer)
{
rt=pt[i]-timer;
strcpy(p[n],p[i]);
pt[n]=rt;
et[i]=timer;
n++;
}
else
{
et[i]=pt[i];
}
i++;
wt[i]=wt[i-1]+et[i-1];
}while(i<n);

count=0;
for(i=0;i<m;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(p[i],p[j])==0)
{
count++;
found=j;
}
}
if(found!=0)
{

wt[i]=wt[found]-(count*timer);
count=0;
found=0;
}
}
for(i=0;i<m;i++)
{
totwt+=wt[i];
}
avgwt=(float)totwt/m;
for(i=0;i<m;i++)
{
printf("\n%s\t%d\t%d",p[i],pt[i],wt[i]);
}
printf("\nTotal waiting time %d\n",totwt);
printf("\nTotal avg.turnaround time: %f",avgwt);
getch();
}

C : secant method


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define E 0.00001
#define f(x) x*x-4*x-10
int main()
{
float x1,x2,x3,f1,f2;
printf("Enter two value (x1,x2) : ");
scanf("%f",&x1);
scanf("%f",&x2);
f1=f(x1);
f2=f(x2);
Label:
x3=(f2*x1-f1*x2)/(f2-f1);
if(fabs((x3-x2)/x3)>E)
{
x1=x2;
f1=f2;
x2=x3;
f2=f(x3);
goto Label;
}
else
printf("The root is %f",x3);
getch();
return(0);
}

C : newton rapson method


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define f(x) x*x-3*x+2
#define df(x) 2*x-3
#define E 0.00001

int main()
{
float x0,x1,y0,dy0;
int i,n;
printf("Enter the value of x : ");
scanf("%f",&x0);
printf("\nEnter the number of iteration : ");
scanf("%d",&n);
printf("\nIter.\tx0\tf(x0)\tdf(x0)\tx1\n");

for(i=0;i<n;i++)
{
y0=f(x0);
dy0=df(x0);
x1=(x0-(y0/dy0));
x0=x1;
printf("%d\t%.4f\t%.4f\t%.4f\t%.4f",i,x0,y0,dy0,x1);
}
if((fabs(x1-x0)/x1)<E)
   {
printf("\nNumber of iteration is %d",i);
printf("\nRoot = %f\n",x1);

}

getch();
return(0);

}

C++ : lagrangue's interpolation


#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
int i,j,n;
float mult,sum=0,x[10],f[10],a;
cout<<"Enter the number of sample points : ";cin>>n;
for(i=0;i<n;i++)
{
        cout<<"( x["<<i<<"],f["<<i<<"] ) : ";
cin>>x[i]>>f[i];
}

cout<<"\nEnter the value of x : ";
cin>>a;

for(i=0;i<=n;i++)
{
mult=1;
for(j=0;j<=n;j++)
{
if(j!=1)
mult=mult*((a-x[j])/(x[i]-x[j]));
}
sum=sum+mult*f[i];
}
cout<<"\nThe value of f(x) = "<<sum;
getch();
return(0);
}

C : fixed point iteration method


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>

#define E 0.00001
#define g(x) 2-x*x

int main()
{
float x1,x2;

printf("Enter the initial guess : ");
scanf("%f",&x1);

Lbl:
x2=g(x1);
if(((x2-x1)/x2)<E)
{
printf("The root is %f",x2);
goto End;
}
else
   {
x1=x2;
goto Lbl;
}
End:
getch();
return(0);
}

C : bisection method


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define E 0.00001
#define f(x) x*x-4*x-10
int main()
{
float x0,x1,y0,y1,x2,y2;
int i=0;
printf("enter the initial guess\n\n");
scanf("%f%f", &x0,&x1);
y0=f(x0);
y1=f(x1);
if((y0*y1)>0)
{
printf("initial guess are not suitable \n\n");
getch();
exit(0);
}
printf("iter\tx0\tf(x0)\tx1\tf(x1)\tx2\tf(x2)\n");
while(fabs((x1-x0)/x1)>E)
{
x2=(x0+x1)/2;
y2=f(x2);
i=i+1;
printf("%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t\n",i,x0,y0,x1,y1,x2,y2);
if(y0*y2>0)
x0=x2;
else
x1=x2;
}
printf("number of iteration is %d\n",i);
printf("the root is %.2f",x2);
getch();
return(0);
}

C++ : matrix display using class


#include<iostream>
#include<conio.h>

using namespace std;

class matrix
{
int a;
int b;
int A[3][3];
public:
void readmatrix();
void displaymatrix();
};

void matrix::readmatrix()
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
cout<<"A["<<i<<"]["<<j<<"] : ";
cin>>A[i][j];
}
}
}
void matrix::displaymatrix()
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
cout<<" "<<A[i][j];
}
cout<<endl;
}

}



int main()
{

matrix m;
m.readmatrix();
m.displaymatrix();
getch();
return(0);
}

C++ : add currency using OOP concept


#include<iostream>
#include<conio.h>

using namespace std;

class currency
{
int rupees;
int paisa;
public:
currency():rupees(0),paisa(0)
{
}
currency(int a):rupees(a),paisa(a)
{
}
currency(int r, int p):rupees(r),paisa(p)
{
}
void getamount()
{
cout<<"Enter rupees : ";cin>>rupees;
cout<<"Enter paisa : ";cin>>paisa;
}
void sum(currency c1, currency c2);
void display();
};

void currency::sum(currency c1, currency c2)
{
paisa=c1.paisa+c2.paisa;
rupees=0;
while(paisa>=100)
{
paisa=paisa-100;
rupees++;
}
rupees+=c1.rupees+c2.rupees;
}

void currency::display()
{
cout<<"Total Sum : \n";
cout<<"Rs. "<<rupees<<" and "<<paisa<<" Paisa";
}

int main()
{
currency c1(70);
currency c3;
currency c2(129,90);
//c1.getamount();
//c2.getamount();
c3.sum(c1,c2);
c3.display();
getch();
return(0);
}

C++ : add complex number using class


#include<iostream>
#include<conio.h>

using namespace std;

class complex
{
private:
int real;
int imaginary;
public:
void setdata(int r,int i)
{
real=r;
imaginary=i;
}
void display()
{
cout<<real<<"+i"<<imaginary;
}
void sum(complex c1,complex c2);
};

void complex::sum(complex c1,complex c2)
{
real=0;
imaginary=0;

real=c1.real+c2.real;
imaginary=c1.imaginary+c2.imaginary;
}

int main()
{
complex c1,c2,c3;
c1.setdata(2,3);
c2.setdata(1,2);
c3.sum(c1,c2);
cout<<"\nFirst complex number =  ";
c1.display();
cout<<"\n\nSecond complex number =  ";
c2.display();
cout<<"\n\n\nSum =  ";
c3.display();
getch();
return(0);
}

C++ : overloading assignment operator +=


#include<iostream>

using namespace std;

class dist
{
private:
    int feet;
    float inches;
public:
    dist():feet(0),inches(0){}
    dist(int f,float in):feet(f),inches(in){}

    void getdist()
    {
        cout<<"feet:";cin>>feet;
        cout<<"inches:";cin>>inches;
    }

    void operator +=(dist d2);
    friend void showdist(dist d);
};

void showdist(dist d)
{
    cout<<d.feet<<"\'-"<<d.inches<<"\""<<endl;
}

void dist::operator+=(dist d2)
{
    feet=feet+d2.feet;
    inches+=d2.inches;
    if(inches>=12)
    {
        inches-=12;
        feet++;
    }
}

int main()
{
    dist d1(7,7.25);
    dist d2;
    d2.getdist();
    d1+=d2;
    cout<<"_______________________________________\n\n\n";
    cout<<"d1 after addition with d2 is:";
    showdist(d1);
    dist d3(4,7.5);
    d1+=d3;//if d1+=d2+d3 is written then '+' should be overloaded
    cout<<"d1 after addition with d3 is:";
    showdist(d1);
    return 0;
}

C++ : overloading insertion and extraction operator


#include<iostream>
#include<conio.h>

using namespace std;

class time1
{
private:
int hrs;
int mins;

public:
time1():hrs(0),mins(0){}
time1(int h, int m):hrs(h),mins(m){}
friend istream & operator >> (istream &tin, time1 &t);
friend ostream & operator << (ostream &tout, time1 &t);
};

istream & operator >>(istream &tin, time1 &t)
{
tin>>t.hrs;
tin>>t.mins;
return tin;
}

ostream & operator << (ostream &tout, time1 &t)
{
tout<<t.hrs<<" : "<<t.mins<<endl;
return tout;
}



int main()
{
time1 t1;
cout<<"Enter time t1 : ";cin>>t1;
cout<<"Time t1 = "<<t1;
time1 t2(11,23);
cout<<"Time t2 is "<<t2;
getch();
return(0);
}

C++ : input n distance and find their average


#include<iostream>
#include<conio.h>
using namespace std;
class dist
{
private:
int feet;
float inches;
public:
dist():feet(0),inches(0){}
dist(int f,float in):feet(f),inches(in){}
void getdist()
{
cout<<"feet = ";cin>>feet;
cout<<"inches = ";cin>>inches;
}
void showdist()
{
cout<<feet<<"\'-"<<inches<<"\""<<endl;
}
dist operator +(dist d2);
dist operator /(int n);
};

dist dist::operator +(dist d2)
{
int f=feet+d2.feet;
float in=inches+d2.inches;
if(in>=12)
{
in-=12;
f++;
}
return dist(f,in);
}

dist dist::operator /(int n)
{
float fltfeet=feet+inches/12;
fltfeet/=n;
int f=int(fltfeet);
float in=12*(fltfeet-f);
return dist(f,in);
}

int main()
{
int n;
dist tot,d[10];
cout<<"Number of distance: ";cin>>n;
cout<<"Enter "<<n<< " distance:\n";
for(int i=0;i<n;i++)
{
cout<<"Distance "<<i+1<<" is: "<<endl;
d[i].getdist();
tot=tot+d[i];
}
dist avg=tot/n;
cout<<"Distance given:\n";
for(int i=0;i<n;i++)
{
cout<<"Distance "<<i+1<<"is :";
d[i].showdist();
}
cout<<"Total distance is: ";
tot.showdist();
cout<<"Average distance is: ";
avg.showdist();
getch();
return 0;

}