The document provides code examples for calculating factorial, GCD, sum of digits, and power of a number using recursion in C programming. It includes the full source code for each program that takes inputs, calls recursive functions to calculate the values, and prints outputs. The recursive functions break down each problem into sub-problems until a base case is reached, calling itself with different arguments each time to ultimately solve the original problem.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
23 views
Recursion Programs
The document provides code examples for calculating factorial, GCD, sum of digits, and power of a number using recursion in C programming. It includes the full source code for each program that takes inputs, calls recursive functions to calculate the values, and prints outputs. The recursive functions break down each problem into sub-problems until a base case is reached, calling itself with different arguments each time to ultimately solve the original problem.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4
1.
FIND FACTORIAL OF A NUMBER USING RECURSION IN C PROGRAM
1. Factorial program by recursion in c
2. Factorial program in c using recursion 3. C program to calculate factorial using recursion 4. Recursive function for factorial in c #include<stdio.h> int fact(int); int main(){ int num,f; printf("\nEnter a number: "); scanf("%d",&num); f=fact(num); printf("\nFactorial of %d is: %d",num,f); return 0; } int fact(int n){ if(n==1) return 1; else return(n*fact(n-1)); }
2. FIND GCD OF A NUMBER USING RECURSION IN C PROGRAM
Find gcd of a number using recursion in c program
#include<stdio.h> int main(){ int n1,n2,gcd; printf("\nEnter two numbers: "); scanf("%d %d",&n1,&n2); gcd=findgcd(n1,n2); printf("\nGCD of %d and %d is: %d",n1,n2,gcd); return 0; } int findgcd(int x,int y){ while(x!=y){ if(x>y) return findgcd(x-y,y); else return findgcd(x,y-x); } return x; }
3. FIND SUM OF DIGITS OF A NUMBER USING RECURSION USING C PROGRAM
Sum of digits in c using recursion
#include<stdio.h> int main(){ int num,x; clrscr();
printf("\nEnter a number: ");
scanf("%d",&num); x=findsum(num); printf("Sum of the digits of %d is: %d",num,x); return 0;
int r,s; int findsum(int n){ if(n){ r=n%10; s=s+r; findsum(n/10); } else return s; }
4. FIND POWER OF A NUMBER USING RECURSION USING C PROGRAM
Find power of a number using recursion using c program
#include<stdio.h> int main(){ int pow,num; long int res; long int power(int,int); printf("\nEnter a number: "); scanf("%d",&num); printf("\nEnter power: "); scanf("%d",&pow); res=power(num,pow); printf("\n%d to the power %d is: %ld",num,pow,res); return 0;
5.
int i=1; long int sum=1; long int power(int num,int pow){ if(i<=pow){ sum=sum*num; power(num,pow-1); } else return sum; }