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