Functions in C
// Simple program of function
void printline();
#include
void main()
{
printline();
printf(“\n It is The Programming \n”);
printline();
getch();
}
void printline(void)
{
int i;
for(i=1;i<23;i++)
{
printf(“-“);
}
}
Output:
———————–
It is The Programming
———————–
// Sum of 1 to n number using function
#include
void main()
{
int addn(int);
int n,ans;
printf(“Enter value n:>>”);
scanf(“%d”,&n);
ans=addn(n);
printf(“Answer=%d,ans”);
getch();
}
int addn(int m)
{
int total=0,i;
for(i=1;i>5
Answer=15
/*Sum of N numbers by recursion*/
#include
int main(){
int n,sum;
printf(“Enter the value of n: “);
scanf(“%d”,&n);
sum = getsum(n);
printf(“Sum of n numbers: %d”,sum);
return 0;
}
int getsum(n){
static int sum=0;
if(n>0){
sum = sum + n;
getsum(n-1);
}
return sum;
}
Output:
Enter the value of n: 10
Sum of n numbers: 55
// Function with argument with return type
int soo(int);
#include
#include
void main()
{
int n,ans;
clrscr();
printf(“\n\n Enter The Value of N:>>”);
scanf(“%d”,&n);
ans=soo(n);
printf(“\n The Sum is :>>%d”,ans);
getch();
}
int soo(int i)
{
int j,k=0;
for(j=1;j>
2
1
C Programs. Get More Example from
https://play.google.com/store/apps/details?id=com.kapidhvaj.cprograms
// Function with argument with return type
int sos(int);
#include
void main()
{
int n,ans;
printf(“\n Enter The Value of N:>>”);
scanf(“%d”,&n);
ans=sos(n);
printf(“\n The Sum is:>>%d”,ans);
}
int sos(int i)
{
int j=1,k=0;
for(j=1;j>
5
The Sum is:>>25
// Find larger value using function
int mx(int,int,int);
#include
void main()
{
int a,b,c,ans;
printf(“\n Enter The Value of A,B,C:>>\n”);
scanf(“%d%d%d”,&a,&b,&c);
ans=mx(a,b,c);
printf(“The Maximum Number Is:>>%d”,ans);
}
int mx(int i,int j,int k)
{
int g;
g=((ik)?i:k):(j>k)?j:k);
return g;
}
Output:
Enter The Value of A,B,C:>>
85
69
87
The Maximum Number Is:>>87
// Find x^y using function
float po(int,int);
#include
void main()
{
int x,y;
float ans;
printf(“\n Enter The Value of X,Y\n”);
scanf(“%d%d”,&x,&y);
ans=po(x,y);
printf(“\n Your Answer is:>>%.2f”,ans);
}
float po(int a,int b)
{
float k=1;
if(b>0)
{
while(b>0)
{
k=k*a;
b–;
}
}
return k;
}
Output:
Enter The value of x,y
5
2
Your Answer is:>>25.00
// Find factorial using function
int fact(int);
#include
void main()
{
int ans,n;
printf(“\n\n\t\tEnter The Value of N:>>”);
scanf(“%d”,&n);
ans=fact(n);
printf(“\n\t\t Your Factorial is:>>%d”,ans);
}
int fact(int n)
{
int facto=1;
if(n==1)
{
return facto;
}
else
{
facto=n*fact(n-1);
return(facto);
}
}
Output:
Enter The Value of N:>>5
Your Factorial is:>>120
// Find number is prime or not using function
int pri(int)
#include
void main()
{
int n,s;
printf(“\n\n\t\t Enter The Value of N:>>”);
scanf(“%d”,&n);
if(pri(n)==1)
{
printf(“\n\t\t\t%d is Prime”,n);}
}
else
{
printf(“\n\t\t\t%d is Not Prime”,n);
}
}
int pri(int m)
{
int i;
for(i=1;i>4
4 is Prime
/*Find the NCr value by using recursive function*/
#include
int main(){
int n,r,ncr;
printf(“Enter any two numbers->”);
scanf(“%d %d”,&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf(“The NCR factor of %d and %d is %d”,n,r,ncr);
return 0;
}
int fact(int n){
int i=1;
while(n!=0){
i=i*n;
n–;
}
return i;
}
Output:
Enter any two numbers-> 5 2
The NCR factor of 5 and 2 is 10