Chapter:
1. Program to find Factorial of a Number using Recursion
Below is a program for finding factorial of a given number using recursion.
Source code:
#include
// declaring the function
int fact(int);
int main()
{
int num, f;
printf("\n\nEnter a number: ");
scanf("%d", &num);
f= fact(num);
printf("\n\nFactorial of %d is %d\n\n", num, f);
return 0;
}
int fact(int aj)
{
if(aj==1 || aj==0)
return 1;
else
return (aj*fact(aj-1));
}
2. C Program to find GCD of two Numbers using Recursion
Greatest Common Divisor(GCD) of two numbers is a number that divides both of them.
Below is a program to the GCD of the two user input numbers using recursion.
Source code:
#include
// declaring the recursive function
int find_gcd(int , int );
int main()
{
int a, b, gcd;
printf("\n\nEnter two numbers to find GCD of \n");
scanf("%d%d", &a, &b);
gcd = find_gcd(a, b);
printf("\n\nGCD of %d and %d is: %d\n\n", a, b, gcd);
return 0;
}
// defining the function
int find_gcd(int x, int y)
{
if(x > y)
find_gcd(x-y, y);
else if(y > x)
find_gcd(x, y-x);
else
return x;
}
3. C Program to Print Binary Equivalent of an Integer using Recursion
Problem Description
This C program, using recursion, finds the binary equivalent of a decimal number entered by the user.
Problem Solution
Decimal numbers are of base 10 while binary numbers are of base 2.
Program/Source Code
#include
int binary_conversion(int);
int main()
{
int num, bin;
printf("Enter a decimal number: ");
scanf("%d", &num);
bin = binary_conversion(num);
printf("The binary equivalent of %d is %d\n", num, bin);
}
int binary_conversion(int num)
{
if (num == 0)
{
return 0;
}
else
{
return (num % 2) + 10 * binary_conversion(num / 2);
}
}
Program Explanation
In this C program, we are reading a decimal number using num variable. Decimal numbers are of base 10, while binary numbers are of base 2. The binary_conversion() function is used to find the binary equivalent of a decimal number entered by the user.
In binary_conversion() function, convert the binary number to its equivalent decimal value. If else condition statement is used to check the value of num variable is equal to 0. If the condition is true, execute the statement by returning 0 to the called variable bin.
Otherwise if the condition is false, execute else statement. Compute the modulus of the value of num variable by 2 and add the resulted value to 10. Multiply the resulted value with the value of binary_conversion() function. Divide the value of num variable by 2 and pass as an argument and execute the function recursively. Print the Binary equivalent of an integer using recursion.
Output:
Enter a decimal number: 10
The binary equivalent of 10 is 1010
4. Program to find Palindrome using Recursion
A Palindrome is a sequence that if reversed looks identical to the original sequence Eg : abba, level, 999 etc.
Below is a simple C program to find whether the user input number is a palindrome or not using recursion:
Source code:
#include
// declaring the recursive function
int isPal(int );
/*
global declaration to use the same value
in both the functions
*/
int n;
int main()
{
int palindrome;
printf("\n\nEnter a number to check for Palindrome: ");
scanf("%d", &n);
palindrome = isPal(n);
if(palindrome == 1)
printf("\n\n\n%d is palindrome\n\n", n);
else
printf("\n\n\n%d is not palindrome\n\n", n);
return 0;
}
int isPal(int aj)
{
static int sum = 0;
if(aj != 0)
{
sum = sum *10 + aj%10;
isPal(aj/10); // recursive call same as while(n!=0) using loop
}
else if(sum == n)
return 1;
else
return 0;
}
5. C Program to find wher a Number is Prime Or Composite using Recursion
Prime Number: A number that is only divisible by 1 and itself.
Composite Number: A number that is not a prime number.
Note: 1 is neither prime nor composite.
Below is a program to find whether the user input number is a prime number or a composite number using recursion.
Source code:
#include
// declaring the recursive function
int isPrime(int, int);
int main()
{
int num, prime;
printf("Enter a positive number to check if Prime: ");
scanf("%d", &num);
prime = isPrime(num, num/2);
if(prime == 1)
{
printf("\n\n%d is a prime number\n\n", num);
}
else
{
printf("\n\n%d is a Composite number\n\n", num);
}
return 0;
}
// function definition
int isPrime(int n, int i)
{
if(i == 1)
return 1; // return statement terminates the recursive funtion
else
{
if(n%i == 0)
return 0;
else
isPrime(n, i-1); // recursive call not using return statement
}
}
6. C Program to Find Reverse of a Number using Recursion
Problem Description
This C program finds the reverse of a number using recursion.
Problem Solution
The following C program using recursion reverses the digits of the number and displays it on the output of the terminal. Eg: 123 becomes 321.
Program/Source Code
#include
#include
int rev(int, int);
int main()
{
int num, result;
int length = 0, temp;
printf("Enter an integer number to reverse: ");
scanf("%d", &num);
temp = num;
while (temp != 0)
{
length++;
temp = temp / 10;
}
result = rev(num, length);
printf("The reverse of %d is %d.\n", num, result);
return 0;
}
int rev(int num, int len)
{
if (len == 1)
{
return num;
}
else
{
return (((num % 10) * pow(10, len - 1)) + rev(num / 10, --len));
}
}
Program Explanation
In this C program, we are reading the integer number using the num variable. Assign the value of num variable to temp variable. While loop is used to check the condition the value of temp variable is not equal to 0, if the condition is true execute the statement divide the value of temp variable by 10.
The result variable is used to call the rev() function by passing num and length variable value as argument. The function rev() is used to reverse the digits of the number. If else condition statement is used to check the value of len variable is equal to 1. If the condition is true execute the statement.
Otherwise, if the condition is false execute the statement. Compute the modulus of the value of num variable by 10 integer and multiply the resulted value with 10. Compute the power of the value of len variable using pow() function. Add the resulted value num variable with 10. Print the reverse of a number using recursion.
Output:
Enter an integer number to reverse: 1234
The reverse of 1234 is 4321.
7. C Program to Find wher a Number is Prime or Not using Recursion
This is a C program to find whether a number is prime or not using recursion.
Problem Description
The following C program, using recursion, finds whether the entered number is a prime number or not.
Problem Solution
A prime number is an integer that has no integral factor but itself and 1.
Program/Source Code
#include
int primeno(int, int);
int main()
{
int num, check;
printf("Enter a number: ");
scanf("%d", &num);
check = primeno(num, num / 2);
if (check == 1)
{
printf("%d is a prime number\n", num);
}
else
{
printf("%d is not a prime number\n", num);
}
return 0;
}
int primeno(int num, int i)
{
if (i == 1)
{
return 1;
}
else
{
if (num % i == 0)
{
return 0;
}
else
{
return primeno(num, i - 1);
}
}
}
Program Explanation
In this C program, we are reading the integer number using num variable. A prime number is an integer that has no integral factor but itself and 1. The check variable is used to call the primeno() function by passing the value of num variable and the value of division of num variable value by 2 as an argument.
The primeno() function is used to find whether the entered number is a prime number or not. If else condition statement is used to check the value of i variable is equal to 1 and return the value of i variable to the called variable check.
Otherwise, if the condition is false execute the else statement and call the primeno() function by passing the value of num variable and the decrement the value of i variable by 1. Return the resulted value to the called variable check.
If else condition statement is used to check that the value of check variable is equal to 1. If the condition is true print the statement as prime number. Otherwise, if the condition is false print the statement as not a prime number.
Output:
Enter a number: 456
456 is not a prime number
Enter a number: 89
89 is a prime number
8. Program to print Fibonacci Series using Recursion
A Fibonacci series is defined as a series in which each number is the sum of the previous two numbers with 1, 1 being the first two elements of the series.
static keyword is used to initialize the variables only once.
Below is a program to print the fibonacci series using recursion.
Source code:
#include
// declaring the function
void printFibo(int );
int main()
{
int k, n;
long int i = 0, j = 1;
printf("Enter the length of the Fibonacci series: ");
scanf("%d", &n);
printf("\n\nfirst %d terms of Fibonacci series are:\n\n\n",n);
printf("%d ", 1);
printFibo(n);
return 0;
}
void printFibo(int aj)
{
static long int first = 0, second = 1, sum;
if(aj > 1)
{
sum = first + second;
first = second;
second = sum;
printf("%ld ", sum);
printFibo(aj-1); // recursive call
}
else
{
// after the elements, for line break
printf("\n\n\n");
}
}
9. C Program to calculate a Number raised to Power of N using Recursion
Below is a program to calculate the result of a given number, raised to the power of n using recursion.
Source code:
#include
// function prototype declaration
int power(int n1, int n2);
int main()
{
int base, exp;
printf("Enter base number: ");
scanf("%d", &base);
printf("\n\nEnter Power factor: ");
scanf("%d", &exp);
printf("\n\n\n\t\t\t%d^%d = %d", base, exp, power(base, exp));
return 0;
}
int power(int b, int e)
{
if(e == 0)
return 1;
return (b*power(b, e-1));
}
10. C Program to Find Sum of N Numbers using Recursion
This is a C program to find sum of first N numbers using recursion.
Problem Description
The following C program using recursion displays the first N natural number on the terminal.
Problem Solution
The user enters the Nth number as the input, the program then calculates the sum of first N numbers using recursion and then displays the final result.
Program/Source Code
#include
void display_sum(int);
int main()
{
int num;
printf("Enter the Nth number: ");
scanf("%d", &num);
display_sum(num);
return 0;
}
void display_sum(int num)
{
static int sum = 0;
if (num == 0)
{
printf("Sum of first N numbers is %d\n", sum);
return;
}
else
{
sum += num;
display_sum(--num);
}
}
Program Explanation
In this C program, we are reading the integer number using the num variable. To find Sum of N Numbers using Recursion, call the display_sum() by passing the num variable value as argument.
In function display_sum(), initialize the value of sum variable with 0 value. Here, sum variable is defined as static so that only one copy of that object will be there upon repeated invocation of that function. If else conditional statement is used to check the value of num variable. If the value of num variable is non zero, we will increment the value of sum variable by num and then call display_sum() recursively by reducing the value of num variable by 1.
Once num becomes 0, we know that we have completed the recursion and we will display the final result stored in the sum variable.
Output:
Enter the Nth number: 3
Sum of first N numbers is 6
Enter the Nth number: 5
Sum of first N numbers is 15
All Chapters
C Programs on integers
C programs on various integers
Recursive program in c
Arrays
- c programms on arrays
- complete IOE solution on arrays
- Detailed description
- And much more
Strings in C programming
- c programms on Strings
- complete IOE solution on Strings
- Detailed description
- Each one using pointer Concepts
- And much more
Data Files
- C programming concepts of data files and data structures
- IOE C Programming Solutions
- Important Numerous C Program
Guest