Thursday , September 21 2017
Home / C / C program to Calculate Sum of Digits of a Number

C program to Calculate Sum of Digits of a Number

C Program to find Digit Sum:  The digit sum of a given integer number is the sum of all its digits. The source code given here are for two methods or rather tricks in C language.

Digit Sum Example:-  

Digit Sum of 12321 is calculated 1+2+3+2+1=9.
Digit Sum of 34236789754 is calculated 3+4+2+3+6+7+8+9+7+5+4=58.

We have also given the Solution of the problem Digit Sum Java Source code with two methods.

Digit Sum Iteration 1 :

  • 1894%10 = 4
  • 1894/10= 189
  • value of S = 0+ 4

Digit Sum Iteration 2 :

  • 189%10=9
  • 189/10= 18
  • Value of S=4+9

Digit Sum Iteration 3 :

  • 18%10=8
  • 18/10=1
  • Value of S=13+8

Digit Sum Iteration 4 :

  • 1%10=1
  • 1/10=0
  • Value of S= 21+1

Digit Sum Iteration 5 :

  • Terminates because of false Condition of loop.
  • #include <stdio.h>
    #include <conio.h>
     
    int main()
    {
       int a, k, sum = 0, rem;
     
       printf("Enter an integern");         //input an integer to calculate digit sum
       scanf("%d", &a);
     
       k = a;
     
       while (k != 0)     \calculate digit sum
       {
          rem = k % 10;
          sum       = sum + rem;
          k         = k / 10;
       }
     
       printf("Sum of digits of %d = %dn", a, sum);   //output of digit sum
     
       return 0;
    }
    
    

     Output:-

    DigitSum

  • #include <stdio.h>
    #include <conio.h>
     
    int main()
    {
       int a, s, k;
       char c[1000];
     
       printf("Input an integern");        //input an integer to calculate digit sum
       scanf("%s", &c);
     
       s = a = 0;
     
       while (c[a] != '')
       {
          t   = c[a] - '0';        // Converting character to integer
          s = s + k;
          a++;
       }
     
       printf("Sum of digits of %s = %dn", c, s);     //output of digit sum
     
       return 0;
    }

    Output:-
    Input an integer
    123456789123456789
    Sum of digits of 123456789123456789 = 90

  • #include <stdio.h>
    
    int add_digits(int);
     
    int main() 
    {
      int a, res;
      printf("Input an integern");           //input an integer to calculate digit sum
      scanf("%d", &a);
      res = add_digits(a);
      printf("Digit sum=%dn", res);
      return 0;
    }
     
    int add_digits(int a)     //calculate digit sum
     {
      static int s = 0;
      if (a == 0) {
        return 0;
      }
     
      s = a%10 + add_digits(a/10);
      return s;
    }

    Output:-

    Input an integer
    12321
    Digit Sum=9

  • Just One line solution for Sum of the digits

    int num = 12346;
    int sum = 0;
    for (int n = num; n > 0; sum += n % 10, n /= 10) ;
  • Digit Sum Program in C

    #include<Math.h>
    #include<stdio.h>
    public static int SumDigits(int value)
     {
         int sum = 0;
         while (value != 0)
         {
             int remainder;
             value = Math.DivRem(value, 10, out remainder);
             sum += remainder;
         }
         return sum;
     }

Check Also

How to Solve Linear Equation in One Variable In C Programming?

C Program to Solve any Linear Equation in One Variable Find complete C program to ...

Leave a Reply

Your email address will not be published. Required fields are marked *