FUNCTIONS
NAME-G.SAI KIRAN
ROLL-911822
1. Write a program to find the square of any number using the function.
#include <iostream>
using namespace std;
void sq(int n);
int main()
int n;
cout <<"enter the value of n:=>\n";
cin >>n;
sq(n);
return 0;
void sq(int n)
float a;
a=n*n;
cout <<"square of a number you enterd is:=>\t"<<
2. Write a program to swap two numbers using function.
#include <iostream>
using namespace std;
void swap(int a,int b);
int main()
int a,b;
cout <<"enter the number which you want to swap a and b:=>\t";
cin >>a>>b;
swap(a,b);
}
void swap(int a,int b)
int c;
c=a;
a=b;
b=c;
cout <<"the number after swapping is:=>\t"<<a<<b;
3. Write a program to check a given number is even or odd using the function.
#include <iostream>
using namespace std;
void check(int n);
int main()
int n;
cout <<"enter hte number which you want to check:=>\t";
cin >>n;
check(n);
void check(int n)
if(n%2==0)
cout <<"\nthe given number is even number:"<<n;
else
cout <<"the givin number odd number:"<<n;
}
4. Write a program to find the sum of the series 1!/1+2!/2+3!/3+4!/4+5!/5 using
the function.
#include<iostream>
using namespace std;
int sum(int n);
int main()
int n=5;
cout<<"the sum of the series is";
cout<<sum(n);
}
int sum(int n)
int i,fact=1,ans=0,term;
for(i=1;i<=n;i++)
fact=fact*i;
term=fact/i;
ans=ans+term;}
return ans;}
5. Write a program to convert decimal number to binary number using the
Function.
#include<iostream>
using namespace std;
int binary(int);
int main()
{
int a;
cout<<"enter the positive integer ";
cin>>a;
cout<<"\n"<<binary(a);
return 0;
int binary(int x)
int a,b=1,c,d=0,r,n,j,k;
c=x;
while(c>0)
r=c%2;
d=d+r*b;
b=b*10;
c=c/2;
return d;
}
6. Write a program to check whether a number is a prime number or not using
the function.
#include<iostream>
using namespace std;
void check(int a);
int main()
int a;
cout<<"enter any number";
cin>>a;
check(a);
}
void check(int a)
int i;
for(i=2;i<a;i++)
if(a%i==0)
cout<<"the number is not prime number";
break;}}
if(i==a)
cout<<"the number is a prime";
7. Write a program to get the largest element of an array using the function.
#include<iostream>
using namespace std;
int max(int arr[50],int size);
int main()
int i,size,arr[50];
cout<<"enter the size of array";
cin>>size;
cout<<"enter the elements in array";
for(i=0;i<size;i++)
cin>>arr[i];
cout<<"largest number in a array is";
cout<<max(arr,size);
int max(int arr[50],int size)
{
int ans,j;
ans=arr[0];
for(j=1;j<size;j++)
if(arr[j]>ans)
ans=arr[j];}
return ans;
}
8. Write a program to check armstrong and perfect numbers using the
function.
#include<iostream>
using namespace std;
void arm(int n)
int sum=0, temp=n;
while (n>0)
sum=sum+(n%10)*(n%10)*(n%10);
n=n/10;
if (temp==sum)
cout<<"\nGiven input is an armstrong number.\n ";
else
cout<<"\nGiven input is not an armstrong number.\n ";
void per(int n)
int sum=0,temp=n;
for (int i=1; i<n ; i++)
if (n%i==0)
sum=sum+i;
if (temp==sum)
cout<<"Given input is a perfect number.\n ";
else
cout<<"Given input is not a perfect number.\n ";
int main()
int n;
cout<<"Enter an element : ";
cin>>n;
per(n);
arm(n);
return 0:
}
9. Write a program to print all perfect numbers in given range using the
function.
#include<iostream>
using namespace std;
void perfect(int a,int b);
int main()
int a,b;
cout<<"enter the lower and upper limit";
cin>>a>>b;
perfect(a,b);
void perfect(int a,int b)
int i,sum=0,j=1;
cout<<"perfect numbers are";
for(i=a;i<=b;i++)
j=1,sum=0;
while(j<i)
if(i%j==0)
sum=sum+j;
}j++;}
if(sum==i)
cout<<i<<endl;