Name-G.Sai Kiran ROLL-911822: Functions
Name-G.Sai Kiran ROLL-911822: Functions
NAME-G.SAI KIRAN
ROLL-911822
1. Write a program to find the square of any number using the function.
#include <iostream>
int main()
int n;
cin >>n;
sq(n);
return 0;
void sq(int n)
float a;
a=n*n;
#include <iostream>
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;
3. Write a program to check a given number is even or odd using the function.
#include <iostream>
using namespace std;
int main()
int n;
cin >>n;
check(n);
void check(int n)
if(n%2==0)
else
}
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>
int main()
int n=5;
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;}
Function.
#include<iostream>
int binary(int);
int main()
{
int a;
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>
int main()
int a;
cin>>a;
check(a);
}
void check(int a)
int i;
for(i=2;i<a;i++)
if(a%i==0)
break;}}
if(i==a)
7. Write a program to get the largest element of an array using the function.
#include<iostream>
using namespace std;
int main()
int i,size,arr[50];
cin>>size;
for(i=0;i<size;i++)
cin>>arr[i];
cout<<max(arr,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)
while (n>0)
sum=sum+(n%10)*(n%10)*(n%10);
n=n/10;
if (temp==sum)
else
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)
else
int main()
int n;
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>
int main()
int a,b;
perfect(a,b);
int i,sum=0,j=1;
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;