C-User Defined Function Codes ( 24 Questions Solved)
1. Write a program to find the factorial of a given number using function.
#include<stdio.h>
long factorial(int a);
int main()
{
int n;
long Fact;
printf("Enter the number:");
scanf("%d",&n);
Fact=factorial(n);
printf("The factorial of the %d is %ld",n,Fact);
}
long factorial(int a)
{
int i;
long fact=1;
for(i=1; i<=a; i++)
{
fact=fact*i;
}
return fact;
}
2. Write a program to find the combination using function
#include<stdio.h>
long factorial(int);
int main()
{
int n,r;
long f1,f2,f3,C;
printf("Enter the total number,n and number being
chosen,r:");
scanf("%d%d",&n,&r);
f1=factorial(n);
f2=factorial(n-r);
f3=factorial(r);
C=f1/(f2*f3);
printf("The combination is %ld",C);
return 0;
}
long factorial(int n)
{
int i;
long Fact=1;
for(i=1; i<=n; i++)
{
Fact=Fact*i;
}
return Fact;
}
#include<stdio.h>
int sum(int a, int b);
int main()
{
int a,b,S;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
S=sum(a,b);
printf("The sum is is %d",S);
}
int sum(int a, int b)
{
int SUM;
SUM=a+b;
return SUM;
}
#include<stdio.h>
int greatest(int,int,int);
int main()
{
int a,b,c;
printf("Enter the numbers:");
scanf("%d%d%d",&a,&b,&c);
greatest(a,b,c);
return 0;
}
int greatest(int a,int b,int c)
{
if(a>c && a>b)
printf("Greatest is %d",a);
else if(b>a && b>c)
printf("Greatest is %d",b);
else
printf("Greatest is %d",c);
return 0;
}
#include<stdio.h>
int swap(int,int);
int main()
{
int a,b;
printf("Enter the two number");
scanf("%d%d",&a,&b);
swap(a,b);
return 0;
}
int swap(int a,int b)
{
int x;
x=a;
a=b;
b=x;
printf("The swap value are %d, %d",a,b);
}
#include<stdio.h>
int swap(int *,int*);
int main()
{
int a,b;
printf("Enter the two number");
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("The swap value are %d, %d",a,b);
return 0;
}
int swap(int *a,int *b)
{
int x;
x=*a;
*a=*b;
*b=x;
}
#include<stdio.h>
float Fah(int);
int main()
{
float C,F;
printf("Enter the celsius temperature C:");
scanf("%f",&C);
F=Fah(C);
printf("The Fahrenheit temperature is %f",F);
return 0;
}
float Fah(int C)
{
int F;
F=((C*9/5))+32;
return F;
}
#include<stdio.h>
int hcflcm(int ,int);
int main()
{
int a,b;
printf("Enter the two number");
scanf("%d%d",&a,&b);
hcflcm(a,b);
return 0;
}
int hcflcm(int a,int b)
{
int c,d,hcf,lcm,temp;
c=a;
d=b;
while(b!=0)
{
temp=b;
b=a%b;
a=temp;
}
hcf=a;
lcm=(c*d)/hcf;
printf("The hcf and lcm are %d and %d",hcf,lcm);
}
#include<stdio.h>
int fibonacci(int);
int main()
{
int n;
printf("Enter the number of terms:");
scanf("%d",&n);
fibonacci(n);
}
int fibonacci(int n)
{
int i,first=0,second=1,next;
for(i=0; i<n; i++)
{
if (i<=1)
{
next=i;
}
else
{
next = first + second;
first= second;
second = next;
}
printf("%d ", next);
}
return 0;
}
#include<stdio.h>
int sum(int n)
{
int i,r,sum=0;
for(i=0;n>0;i++)
{
r = n%10;
if(r%2 ==0)
sum = sum+r;
n = n/10;
}
printf("Sum is %d",sum);
}
int main()
{
int n;
printf("Enter a number.\n");
scanf("%d",&n);
sum(n);
}
#include<stdio.h>
long factorial(int a);
int main()
{
int n;
long Fact;
printf("Enter the number:");
scanf("%d",&n);
Fact=factorial(n);
printf("The factorial is %ld",Fact);
}
long factorial(int n)
{
long f;
if(n==0)
{
return 1;
}
else
{
f=n*factorial(n-1);
return f;
}
}
12. Write a program to calculate the fibonacci sequence using recursion
#include<stdio.h>
int fibonacci(int);
int main()
{
int n,i=1;
printf("Enter the number of terms:");
scanf("%d",&n);
while(i<=n)
{
printf("\t%d",fibonacci(i));
i++;
}
return 0;
}
int fibonacci(int n)
{
if(n==1)
return 0;
else if (n==2)
return 1;
else
return(fibonacci(n-1)+fibonacci(n-2));
}
13. Write a program to read a number n from user and calculate sum of first n natural numbers using Recursion
#include<stdio.h>
int sum(int);
int main()
{
int a,Sum;
printf("Enter the number:");
scanf("%d",&a);
Sum=sum(a);
printf("The sum of natural number upto %d is
%d",a,Sum);
return 0;
}
int sum(int a)
{
int SUM=0;
if(a==0)
return 0;
else
SUM=a+sum(a-1);
return SUM;
}
14. Write a program to generate particular term in Fibonacci Series using Recursion
#include<stdio.h>
int Fibonacci(int);
int main()
{
int n,i=1;
printf("Enter the number of terms:");
scanf("%d",&n);
printf("%d",fibonacci(n));
return 0;
}
int fibonacci(int n)
{
if(n==1)
return 0;
else if (n==2)
return 1;
else
return(fibonacci(n-1)+fibonacci(n-2));
}
15. Write a program that will evaluate a floating-point number to be raised to an integer power using Recursion(i.e. y = m^n)
#include<stdio.h>
float power(float,float);
int main()
{
float m,n,pow;
printf("Enter the base,m and exponent,n:");
scanf("%f%f",&m,&n);
pow=power(m,n);
printf("The power is %.2f",pow);
return 0;
}
float power(float base, float exp)
{
if(exp==0)
return 1;
else
return (base*power(base,exp-1));
}
#include<stdio.h>
int reverse(int,int);
int main()
{
int n;
printf("Enter the positive number:");
scanf("%d",&n);
printf("The reversed number is %d",reverse(n,0));
return 0;
}
int reverse(int n,int result)
{
int R;
if(n>0)
{
R=n%10;
result=result*10+R;
reverse(n/10,result);
}
else
return result;
}
#include<stdio.h>
int sum(int,int);
int main()
{
int n,SUM;
printf("Enter the number:");
scanf("%d",&n);
SUM=sum(n,0);
printf("The sum of digits is %d",SUM);
}
int sum(int n,int S)
{
int R;
if(n!=0)
{
R=n%10;
S=S+R;
sum(n/10,S);
}
else
return S;
}
#include<stdio.h>
int prime(int n,int i)
{
int a=1,c=0;
if(i==1)
{
return 1;
}
else if(n%i==0)
{
return 0;
}
else
{
prime(n,i-1);
}
}
int main()
{
int n,c;
printf("Enter a num\n");
scanf("%d",&n);
c = prime(n,n-1);
if(c==1)
printf("It is prime.");
else
printf("It is composite.");
}
19. Write a program to display the prime numbers entered by the user in the range using the concept of Recursion.
#include<stdio.h>
int prime(int n,int i)
{
int a=1,c=0;
if(i==1)
{
return 1;
}
else if(n%i==0)
{
return 0;
}
else
{
prime(n,i-1);
}
}
int main()
{
int n1,n2,c,j;
printf("Enter a number range \n");
scanf("%d%d",&n1,&n2);
for(j=n1;j<=n2;j++)
{
c = prime(j,j-1);
if(c==1)
printf("%d ",j);
}
}
20. Write a program to check whether a number is Armstrong or not using the concept of Recursive Function.
#include<stdio.h>
int Armstrong(int,int);
int main()
{
int n,num,Result;
printf("Enter the number:");
scanf("%d",&n);
num=n;
Result=Armstrong(n,0);
if(Result==num)
printf("Armstrong Number");
else
printf("Not a Armstrong Number");
return 0;
}
int Armstrong(int n,int sum)
{
int R;
if(n!=0)
{
R=n%10;
sum=sum+R*R*R;
Armstrong(n/10,sum);
}
else
return sum;
}
21. Write a program to display the Armstrong number in the range using Recursion.
#include<stdio.h>
int main()
{
int n1,n2,result,num,R,i;
printf("Enter the no:");
scanf("%d%d",&n1,&n2);
printf("Armstrong number between %d and %d
are:",n1,n2);
for(i=n1; i<=n2; i++)
{
num=i;
result=Armstrong(i,0);
if(result==num)
printf("%d\t",i);
}
}
int Armstrong(int n,int sum)
{
int R;
if(n!=0)
{
R=n%10;
sum=sum+R*R*R;
Armstrong(n/10,sum);
}
else
return sum;
}
22. Write a program to check whether a given number is Palindrome or not using the concept of Recursive Function.
#include<stdio.h>
int Palindrome(int, int);
int main()
{
int n,Result,num;
printf("Enter the number:");
scanf("%d",&n);
num=n;
Result=Palindrome(n,0);
if(num==Result)
printf("Palindrome Number");
else
printf("Not a palindrome number");
return 0;
}
int Palindrome(int n, int sum)
{
int R;
if(n!=0)
{
R=n%10;
sum=sum*10+R;
Palindrome(n/10,sum);
}
else
return sum;
}
23. Write a program to find out whether the nth term of the Fibonacci series is a prime or not. Read the value of n from the user and display the result in the main function. User separate user defined functions to generate the nth Fibonacci term and to check whether a number is prime or not.
#include<stdio.h>
int prime(int n,int i)
{
int a=1,c=0;
if(i==1)
{
return 1;
}
else if(n%i==0)
{
return 0;
}
else
{
prime(n,i-1);
}
}
int series(int n)
{
int fib;
if(n==1)
{
return 0;
}
else if(n==2)
{
return 1;
}
else
{
return series(n-1)+series(n-2);
}
}
int main()
{
int n,term,check;
printf("Enter a num\n");
scanf("%d",&n);
term = series(n);
check = prime(term,term-1);
if(check==1)
printf("%dth term %d is prime.",n,term);
else
printf("%dth term %d is not prime.",n,term);
}
24. Write a program to calculate the sum of series given below using Recursive Function. 1 + 11 + 111 + … + up to N terms. If N is read as 5, the series is: 1 + 11 + 111 + 1111 + 11111.
#include<stdio.h>
int Sum(int);
int main()
{
int n;
printf("Enter the value of n:");
scanf("%d",&n);
printf("The sum of the series is %d",Sum(n));
return 0;
}
int Sum(int n)
{
int sum=0,i;
if(n==1)
return 1;
else
{
sum=0;
for(i=0; i<n; i++)
{
sum=sum+pow(10,i);
}
return sum+Sum(n-1);
}
}
Comments
Post a Comment