Sum of Digits using Recursion in C Language

sum-of-digits-using-recursion-in-c-programming

Sum of Digits using Recursion in C Program Description:

Write a Program to calculate the Sum of Digits using Recursion in C Programming language. The program should accept a number from the user and calculate the sum of each individual digit using the recursion. ( Program shouldn’t use the loops)

Example Input and Output:

Input:

Enter a Number : 8734

Output:

Sum of digits of number 8734 is : 22

Pre-Requisites:

We are going to use functions and recursion, So please go through the following articles to learn more about functions and recursion in C.

Sum of Digits using Recursion in C Algorithm/Explanation:

  1. Start the program and accept the input number from the user. Store the input value in the variable named n.
  2. Display an error message if the user enters a negative number and Prompt the user to input again using the Goto Statement
    • Use goto INPUT; to jump the program control to INPUT: label. which we added at the start of the program.
  3. Create a function called sumOfDigits, The sumOfDigits function is going to take one integer number( n) as input and calculates the sum of each individual digit of the number( n) using recursion. Here are the details of the sumOfDigits function.
    • As we are using the recursion, We need to have the base condition. The sumOfDigits function base condition is n == 0. If the number n value becomes zero, Then return the Zero(0). i.e if(n==0) return 0;
    • If the number n is not zero, Then get the last digit of the number using the modulus operator ( n%10)
    • Make a recursive call to sumOfDigits function, pass the number n by removing the last digit using the division operator ( n/10).
    • Finally, Add the above two values – (n % 10) + sumOfDigits(n / 10)
  4. Call the sumOfDigits function from the main function. Pass the input number as the parameter to sumOfDigits function. – sumOfDigits(n). The sumOfDigits function returns the sum of digits of the given number n.
  5. Display the result on the console.

Sum of Digits using Recursion in C Program:

Here is the program to calculate the sum of digits in C using the recursion.

Prototype details of the sumOfDigits function.

  • int sumOfDigits(int);
  • Function_name: sumOfDigits
  • Arguments_list: int
  • Return_type: int
  • Program Description: Calculate the sum of digits of the number ‘n’ and return the result back to the caller.

Program Output:

Compile and Run the program using your IDE. we are using the GCC compiler on Ubuntu Linux.

Test Case 1: When the Positive Numbers are entered by the user

As we can see from the above output, The program generated a valid output.

sum-of-digits-of-a-number-using-recursion-in-c-language-program-output

Test Case 2: When the Negative numbers are entered by the user:

If the user enters a negative number, The program is displaying an error message ( ERROR: Invalid Input, Please provide Positive Number). And the program also allows the user to provide the input again. This process will continue until a valid number is provided by the user.

Recursion Practice Programs:

Functions Practice Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Calculate the Sum of Digits using Recursion […]

Leave a Reply