w3resource

C Programming Exercises, Practice, Solution : Recursion

C Recursion [21 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

1. Write a program in C to print the first 50 natural numbers using recursion.
Expected Output:

 The natural numbers are : 1  2  3
  4  5  6  7  8  9  10  11  12  13
  14  15  16  17  18  19  20  21  
22  23  24  25  26  27  28  29  30
  31  32  33  34  35  36  37  38  
39  40  41  42  43  44  45  46  47
  48  49  50 

Click me to see the solution

2. Write a program in C to calculate the sum of numbers from 1 to n using recursion.
Test Data :
Input the last number of the range starting from 1 : 5
Expected Output:

The sum of numbers from 1 to 5 : 
15

Click me to see the solution

3. Write a program in C to print the Fibonacci Series using recursion. >
Test Data :
Input number of terms for the Series (< 20) : 10
Expected Output:

 Input number of terms for the Series (< 20) : 10                                
 The Series are :                                                                
 1  1  2  3  5  8  13  21  34  55  

Click me to see the solution

4. Write a program in C to print the array elements using recursion. >
Test Data :
Input the number of elements to be stored in the array :6
Input 6 elements in the array :
element - 0 : 2
element - 1 : 4
element - 2 : 6
element - 3 : 8
element - 4 : 10
element - 5 : 12
Expected Output :

The elements in the array are : 2  4  6  8  10  12 

Click me to see the solution

5. Write a program in C to count the digits of a given number using recursion. >
Test Data :
Input a number : 50
Expected Output :

The number of digits in the number is :  2

Click me to see the solution

6. Write a program in C to find the sum of digits of a number using recursion. >
Test Data :
Input any number to find sum of digits: 25
Expected Output:

The Sum of digits of 25 = 7 
 

Click me to see the solution

7. Write a program in C to find the GCD of two numbers using recursion. >
Test Data :
Input 1st number: 10
Input 2nd number: 50
Expected Output :

The GCD of 10 and 50 is: 10 

Click me to see the solution

8. Write a program in C to get the largest element of an array using recursion. >
Test Data :
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 5
element - 1 : 10
element - 2 : 15
element - 3 : 20
element - 4 : 25
Expected Output :

Largest element of an array is: 25  

Click me to see the solution

9. Write a program in C to reverse a string using recursion. >
Test Data :
Input any string: w3resource
Expected Output:

The reversed string is: ecruoser3w   

Click me to see the solution

10. Write a program in C to find the Factorial of a number using recursion. >
Test Data :
Input a number : 5
Expected Output:

The Factorial of 5 is : 120

Click me to see the solution

11. Write a program in C to convert a decimal number to binary using recursion. >
Test Data :
Input any decimal number : 66
Expected Output :

The Binary value of decimal no. 66 is : 1000010    

Click me to see the solution

12. Write a program in C to check if a number is a prime number or not using recursion. >
Test Data :
Input any positive number : 7
Expected Output :

The number 7 is a prime number.   

Click me to see the solution

13. Write a program in C to find the LCM of two numbers using recursion. >
Test Data :
Input 1st number for LCM : 4
Input 2nd number for LCM : 6
Expected Output :

The LCM of 4 and 6 :  12   

Click me to see the solution

14. Write a program in C to print even or odd numbers in a given range using recursion. >
Test Data :
Input the range to print starting from 1 : 10
Expected Output :

All even numbers from 1 to 10 are : 2  4  6  8  10                              
All odd numbers from 1 to 10 are : 1  3  5  7  9   

Click me to see the solution

15. Write a C program to multiply two matrices using recursion. >
Test Data :
Input number of rows for the first matrix : 2
Input number of columns for the first matrix : 1
Input number of rows for the second matrix : 1
Input number of columns for the second matrix : 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [1],[0] : 2
Input elements in the second matrix :
element - [0],[0] : 3
element - [0],[1] : 4
Expected Output :

Here is the elements of First matrix :                                          
                                                                                 
 1                                                                               
 2                                                                               
 Here is the elements of Second matrix :                                         
                                                                                 
 3       4                                                                       
 The multiplication of two matrix is :                                           
                                                                                 
 3       4                                                                       
 6       8    

Click me to see the solution

16. Write a C program to check whether a given string is a palindrome or not using recursion. >
Test Data :
Input a word to check for palindrome : mom
Expected Output :

 The entered word is a palindrome.  

Click me to see the solution

17. Write a program in C to calculate the power of any number using recursion. >
Test Data :
Input the base value : 2
Input the value of power : 6
Expected Output :

The value of 2 to the power of 6 is : 64   

Click me to see the solution

18. Write a C program to find the Hailstone Sequence of a given number up to 1. >
Test Data :
Input any number (positive) to start for Hailstone Sequence : 13
Expected Output :

 The hailstone sequence starting at 13 is :                                                                   
 13  40  20  10  5  16  8  4  2 1                                                                             
 The length of the sequence is 10.

Click me to see the solution

19. Write a program in C to copy one string to another using recursion. >
Test Data :
Input the string to copy : w3resource
Expected Output :

 The string successfully copied.                                                                              
                                                                                                              
 The first string is : w3resource                                                                             
 The copied string is : w3resource

Click me to see the solution

20. Write a program in C to find the first capital letter in a string using recursion. >
Test Data :
Input a string to including one or more capital letters : testString
Expected Output :

 The first capital letter appears in the string testString is S. 

Click me to see the solution

21. Write a program in C for binary search using recursion. >
Test Data :
Input the number of elements to store in the array :3
Input 3 numbers of elements in the array in ascending order :
element - 0 : 15
element - 1 : 25
element - 2 : 35
Input the number to search : 35
Expected Output :

 The search number found in the array.

Click me to see the solution

C Programming Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming-exercises/recursion/index.php