Chapter:
1. 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");
}
}
2. 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
3. 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
4. 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));
}
5. C Program to Find Product of 2 Numbers using Recursion
Problem Description
This C program finds the product of 2 numbers using recursion.
Problem Solution
This C program using recursion, finds the product of 2 numbers without using the multiplication operator.
Program/Source Code
#include
int product(int, int);
int main()
{
int a, b, result;
printf("Enter two numbers to find their product: ");
scanf("%d%d", &a, &b);
result = product(a, b);
printf("Product of %d and %d is %d\n", a, b, result);
return 0;
}
int product(int a, int b)
{
if (a < b)
{
return product(b, a);
}
else if (b != 0)
{
return (a + product(a, b - 1));
}
else
{
return 0;
}
}
Program Explanation
In this C program, reading two numbers using a and b variables respectively. The product() function is used to find the product of two numbers. Nested if else condition statement is used to check the value of a variable is less than the value of b variable.
If the condition is true then execute the statement. Compute the summation of the value of a variable with the value. Otherwise, if the condition is false then execute else if condition statement. Check the condition that the value of b variable is not equal to 0.
If the condition is true then execute the statement. Otherwise, if the condition is false then execute the else statement and return the null. Print the product of two numbers.
Output:
Enter two numbers to find their product: 176 340
Product of 176 and 340 is 59840
6. C Program to Find Sum of Digits of a Number using Recursion
This is a C program to find sum of digits of a number using recursion.
Problem Description
This C program finds the sum of digits of a number using recursion.
Problem Solution
The following C program, using recursion, finds the sum of its digits.
advertisement
Program/Source Code
#include
int sum (int a);
int main()
{
int num, result;
printf("Enter the number: ");
scanf("%d", &num);
result = sum(num);
printf("Sum of digits in %d is %d\n", num, result);
return 0;
}
int sum (int num)
{
if (num != 0)
{
return (num % 10 + sum (num / 10));
}
else
{
return 0;
}
}
Program Explanation
In this C program, we are reading the integer number using the num variable. The function sum() is used to find sum of digits of a number using recursion.
In function sum() check the value of num variable is not equal to 0. If the condition is true execute the statement. Divide the value of num variable by 10 integer value. Add the resulted value along with the modulus of the value of num variable. Print the sum of digits of a number using recursion.
Output:
Enter the number: 2345
Sum of digits in 2345 is 14
7. C Program to find LCM of two Numbers using Recursion
LCM: Least Common Multiple of two numbers is the number that is a common multiple of the both the numbers.
Below is a program to find LCM of two numbers using recursion.
#include
int find_lcm(int, int); // function prototype declaration
int main()
{
int a, b, lcm;
printf("\n\nEnter 2 integers to find LCM of:\n");
scanf("%d%d", &a, &b);
lcm = find_lcm(a,b); // function call
printf("\n\n LCM of %d and %d is: %d\n\n", a, b, lcm);
return 0;
}
int find_lcm(int a, int b) // function definition
{
/*
static variable is initialized only once
for each function call
*/
static int temp = 1;
if(temp%a == 0 && temp%b == 0)
{
return temp;
}
else
{
temp++;
find_lcm(a,b);
return temp;
}
}
8. 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));
}
9. 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;
}
10. 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;
}
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