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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") c/FunctionsGroup -.-> c/recursion("`Recursion`") subgraph Lab Skills c/output -.-> lab-123340{{"`Sum of Digits Using Recursion`"}} c/comments -.-> lab-123340{{"`Sum of Digits Using Recursion`"}} c/variables -.-> lab-123340{{"`Sum of Digits Using Recursion`"}} c/data_types -.-> lab-123340{{"`Sum of Digits Using Recursion`"}} c/operators -.-> lab-123340{{"`Sum of Digits Using Recursion`"}} c/if_else -.-> lab-123340{{"`Sum of Digits Using Recursion`"}} c/user_input -.-> lab-123340{{"`Sum of Digits Using Recursion`"}} c/memory_address -.-> lab-123340{{"`Sum of Digits Using Recursion`"}} c/pointers -.-> lab-123340{{"`Sum of Digits Using Recursion`"}} c/function_parameters -.-> lab-123340{{"`Sum of Digits Using Recursion`"}} c/function_declaration -.-> lab-123340{{"`Sum of Digits Using Recursion`"}} c/recursion -.-> lab-123340{{"`Sum of Digits Using Recursion`"}} end

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