USING FUNCTION
1. Program to calculate factorial of number using function.
#include<stdio.h>
#include<conio.h>
void fact();
void main()
{
fact();
getch();
}
void fact()
{
int n,i,fa=1;
printf("Enter the number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fa=fa*i;
}
printf("%d is factorial",fa);
}
Output:

2. Program for maximum of 5 number
#include<stdio.h>
#include<conio.h>
void maxi();
void main()
{
maxi();
getch();
}
void maxi()
{
int a,b,c,d,e;
printf("enter the value\n");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
if(a>b&&a>c&&a>d&&a>e)
{
printf("%d is maximum",a);
}
else if(b>c&&b>d&&b>e)
{
printf("%d is maximum",b);
}
else if(c>d&&c>e)
{
printf("%d is maximum",c);
}
else if(d>e)
{
printf("%d is maximum",d);
}
else
{
printf("%d is maximum",e);
}
}
Output:

3. Program to find sum of N natural number.
#include<stdio.h>
#include<conio.h>
void sum();
void main()
{
sum();
getch();
}
void sum()
{
int N,n,i;
printf("Enter the number\n");
scanf("%d",&N);
N=N*(N+1)/2;
printf("%d is required sum",N);
}
Output:

4. Program to calculate area of 3 circle.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void area();
void main()
{
area();
area();
area();
getch();
}
void area()
{
int r;
float A;
printf("Enter the radius\n");
scanf("%d",&r);
A=3.14*pow(r,2);
printf("%f is required area\n",A);
}
Output:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void area();
void main()
{
area();
area();
area();
getch();
}
void area()
{
int r;
float A;
printf("Enter the radius\n");
scanf("%d",&r);
A=3.14*pow(r,2);
printf("%f is required area\n",A);
}
Output:
5. Program for fibonacci series using recursive function
#include<stdio.h>
#include<conio.h>
int fibo(int n);
void main()
{
int f,n;
printf("Enter the number\n");
scanf("%d",&n);
f=fibo(n) ;
printf("The fibonacci series of %d is %d",n,f);
getch();
}
int fibo(int n)
{
if(n==1)
return 0;
else if(n==2)
return 1;
else
{
return(fibo(n-1)+fibo(n-2));
}
}
Output:
8. Program to count upper,lower and digits using function.
#include <stdio.h>
#include<conio.h>
void upper();
void main()
{
upper();
getch();
}
void upper ()
{
char str[150];
int i;
int upper,lower,digit;
upper=lower=digit= 0;
printf("Enter the string");
gets(str );
for (int i = 0; str[i]!= '\0';i++)
{
if (str[i]>=65&&str[i]<=90)
{
upper++;
}
else if(str[i]>=90&&str[i]<=122)
{
lower++;
}
else if (str[i]>= '0'&& str[i] <= '9')
{
digit++;
}
}
printf("The upper are%d\nlower are %d\n digit are %d\n",upper,lower,digit);
}
Output:
9. Program to swap the number by call by value in function call.
#include<stdio.h>
#include<conio.h>
void swap(int x,int y);
void main()
{
int x=10,y=20;
printf("Given value of x=%d\nGiven value of y=%d\n",x,y);
swap(x,y);
printf(" To be swapped value of x=%d\n To be swapped value of y=%d\n",x,y);
getch();
}
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("Swapped value of x=%d\n Swapped value of y=%d\n",x,y);
}
10. Program to swap the value by call by reference in function call.
#include<stdio.h>
#include<conio.h>
void swap(int *x,int *y);
void main()
{
int x=10,y=20;
printf("Given value of x=%d\nGiven value of y=%d\n",x,y);
swap(&x,&y);
printf(" To be swapped value of x=%d\n To be swapped value of y=%d\n",x,y);
getch();
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
printf("Swapped value of x=%d\n Swapped value of y=%d\n",*x,*y);
}
#include<stdio.h>
#include<conio.h>
int fibo(int n);
void main()
{
int f,n;
printf("Enter the number\n");
scanf("%d",&n);
f=fibo(n) ;
printf("The fibonacci series of %d is %d",n,f);
getch();
}
int fibo(int n)
{
if(n==1)
return 0;
else if(n==2)
return 1;
else
{
return(fibo(n-1)+fibo(n-2));
}
}
Output:
6.Sum of natural number
#include<stdio.h>
#include<conio.h>
int sum(int n);
void main()
{
int n;
printf("Enter the number\n");
scanf("%d",&n);
printf("The sum is %d",sum(n));
getch();
}
int sum(int n)
{
if(n==1)
{
return 1;
}
else
{
return(n+sum(n-1));
}
}
Output:
7 Program for factorial number
#include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
int n;
printf("Enter the number\n");
scanf("%d",&n);
printf("The factorial is %d",fact(n));
getch();
}
int fact(int n)
{
if(n==1)
{
return 1;
}
else
{
return(n*fact(n-1));
}
}
8. Program to count upper,lower and digits using function.
#include <stdio.h>
#include<conio.h>
void upper();
void main()
{
upper();
getch();
}
void upper ()
{
char str[150];
int i;
int upper,lower,digit;
upper=lower=digit= 0;
printf("Enter the string");
gets(str );
for (int i = 0; str[i]!= '\0';i++)
{
if (str[i]>=65&&str[i]<=90)
{
upper++;
}
else if(str[i]>=90&&str[i]<=122)
{
lower++;
}
else if (str[i]>= '0'&& str[i] <= '9')
{
digit++;
}
}
printf("The upper are%d\nlower are %d\n digit are %d\n",upper,lower,digit);
}
Output:
9. Program to swap the number by call by value in function call.
#include<stdio.h>
#include<conio.h>
void swap(int x,int y);
void main()
{
int x=10,y=20;
printf("Given value of x=%d\nGiven value of y=%d\n",x,y);
swap(x,y);
printf(" To be swapped value of x=%d\n To be swapped value of y=%d\n",x,y);
getch();
}
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("Swapped value of x=%d\n Swapped value of y=%d\n",x,y);
}
10. Program to swap the value by call by reference in function call.
#include<stdio.h>
#include<conio.h>
void swap(int *x,int *y);
void main()
{
int x=10,y=20;
printf("Given value of x=%d\nGiven value of y=%d\n",x,y);
swap(&x,&y);
printf(" To be swapped value of x=%d\n To be swapped value of y=%d\n",x,y);
getch();
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
printf("Swapped value of x=%d\n Swapped value of y=%d\n",*x,*y);
}
11. Program for fibonacci series by recursive function where first digit is 2 .
#include<stdio.h>
#include<conio.h>
int fibo(int n);
void main()
{
int f,n,i;
printf("Enter the number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=fibo(i) ;
printf("%d\t",f);
}
getch();
}
int fibo(int n)
{
if(n==1)
return 2;
else if(n==2)
return 3 ;
else
{
return(fibo(n-1)+fibo(n-2));
}
}
Output:
Comments
Post a Comment