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;
}
}