Sum of Digits Using Recursion

CCBeginner
Practice Now

Introduction

Recursion is a programming technique where a function calls itself, allowing the function to repeat until a base case is met and the function returns the final output. In this lab, we will learn how to find the sum of digits of a given number using recursion.

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Getting User Input

The first step is to get user input, which will be the number we want to find the sum of its digits. To do this, we will use the scanf function to read the user input from the standard input (stdin) stream.

#include<stdio.h>

int main()
{
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);

    /* Add Code */

    return 0;
}

Defining the Recursive Function

Next, we will write a recursive function to find the sum of digits of the given number. The function will take an integer as a parameter and return an integer as the sum of digits. The function will perform the following steps:

  1. If the number is less than 10, the function will return the number itself as the sum of digits.
  2. If the number is greater than or equal to 10, the function will calculate the sum of the last digit and call itself with the remaining digits as an argument.
int sumOfDigits(int num)
{
    if(num < 10)
    {
        return num;
    }
    else
    {
        return num%10 + sumOfDigits(num/10);
    }
}

Calling the Recursive Function

Now, we will call the recursive function defined in the previous step with the user input obtained in Step 1. The function will return the sum of digits of the number as an integer.

#include<stdio.h>

int sumOfDigits(int num);

int main()
{
    int num, sum;
    printf("Enter a number: ");
    scanf("%d", &num);

    sum = sumOfDigits(num);

    /* Add Code */

    return 0;
}

int sumOfDigits(int num)
{
    if(num < 10)
    {
        return num;
    }
    else
    {
        return num%10 + sumOfDigits(num/10);
    }
}

Displaying the Result

The final step is to display the sum of digits of the number obtained in Step 1. We will use printf function to display the result to the standard output (stdout) stream.

#include<stdio.h>

int sumOfDigits(int num);

int main()
{
    int num, sum;
    printf("Enter a number: ");
    scanf("%d", &num);

    sum = sumOfDigits(num);

    printf("Sum of digits of %d is: %d", num, sum);

    return 0;
}

int sumOfDigits(int num)
{
    if(num < 10)
    {
        return num;
    }
    else
    {
        return num%10 + sumOfDigits(num/10);
    }
}

Summary

In this lab, we learned how to find the sum of digits of a given number using recursion. We first obtained user input and defined a recursive function to calculate the sum of digits. We then called the function and displayed the result to the user.

Other C Tutorials you may like