Write a program to sum the digit of a given number in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (log₁₀ n)Space Complexity: O (1)Open Source: MIT
1
2
3
        #include <stdio.h>
4
        #include <conio.h>
5
    
6
        void main()
7
        {
8
            int num, rem, sum = 0, temp;
9
            clrscr();
10
            printf("Enter the number:");
11
            scanf("%d", &num);
12
            temp = num;
13
            while (num != 0)
14
            {
15
                rem = num % 10;
16
                sum += rem;
17
                num = num / 10;
18
            }
19
            printf("The sum of %d decimal digits is %d", temp, sum);
20
        }
21
        

Output

1
2
    Enter the number:123
3
    The sum of 123 decimal digits is 6
4
        

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run sum the digits of a given number program in C-Language?

To run sum the digits of a given number program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More About Sum of Digits of a Number Using C Language

Aim: To calculate the sum of the individual digits of a given number provided by the user. For Eg: if user input is 1234 then the output is 1+2+3+4 = 10.

Concepts used in this program

  • While loop: To process all the digits of the input number.
  • Modulus operator (%): To get the last digit of the number.
  • Division Operator: To remove the digit by integer division.
  • Accumulator (sum): To sum all the stored digits.
  • Temporary Variable (temp): To keep the original number unchanged and to use in the output.

Real-World Applications

  • OCR (Optical Character Recognition): To recognize characters, some part of this program is used for scanned documents.
  • Digital Root of a number: To find out the digital root of a number, this program is directly used.
  • Logic Building: This program is used to improve the logic-building of a programmer or student.

24 → 2 + 4 = 6. This is known as the digital root of a Number.

License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.