Q. Write a program to find the sum of digits in number.



Count Digits :- In this, we have to find the Sum of each digits in number.
For Example :-
input:- 12548
output:- 20

Sum Of Digits Algorithm

START
      Step 1: Get number by user
      Step 2: Get the modulus/remainder of the number
      Step 3: sum the remainder of the number
      Step 4: Divide the number by 10
      Step 5: Repeat the step 2 while number is greater than 0.
STOP


Sum Of Digits Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include <stdio.h>
    int sum_digit(long long num) 
    { 
        int sum = 0,rem; 
        while (num != 0) { 
            rem=num%10;
            num = num / 10; 
            sum=sum+rem; 
        } 
        return sum; 
    } 
      
    // Driver code 
    int main(void) 
    { 
        long long num = 123456; 
        printf("Sum Of Digits : %d", 
               sum_digit(num)); 
        return 0; 
    } 
    
    #include <iostream>
    using namespace std; 
      
    int sum_digit(long long num) 
    { 
        int sum = 0,rem; 
        while (num != 0) { 
            rem=num%10;
            num = num / 10; 
            sum=sum+rem;
        } 
        return sum; 
    } 
      
    // Driver code 
    int main(void) 
    { 
        long long num = 123456; 
        cout << "Sum Of Digits : "
             << sum_digit(num); 
        return 0; 
    } 
    
    class LFC { 
      
        static int sum_digit(int num) 
        { 
            int sum = 0,rem; 
            while (num != 0) { 
                rem=num % 10;
                num = num / 10; 
                sum=sum+rem; 
            } 
            return sum; 
        } 
      
        /* Driver program to test above function */
        public static void main(String[] args) 
        { 
            int num = 123456; 
            System.out.print("Sum Of Digits : " + sum_digit(num)); 
        } 
    } 
    
    
    def sum_digit(num): 
        sum1 = 0
        rem = 0
        while num != 0: 
            rem=num%10
            num //= 10
            sum1=sum1+rem
        return sum1
      
    # Driver Code     
    num = 123456
    print ("Sum Of Digits : % d"%(sum_digit(num))) 
    
    using System; 
      
    class LFC { 
      
        static int sum_digit(int num) 
        { 
            int sum = 0,rem; 
            while (num != 0) { 
                rem=num%10;
                num = num / 10; 
                sum=sum+rem; 
            } 
            return sum; 
        } 
      
        /* Driver program to test 
        above function */
        public static void Main() 
        { 
            int num = 123456; 
            Console.WriteLine("Sum Of"
                              + " Digits : " + sum_digit(num)); 
        } 
    } 
    
    function sum_digit($num) 
    { 
        $sum = 0;
        $rem=0; 
        while ($num != 0)  
        {   $rem=$num%10;
            $num = $num / 10; 
            $sum=$sum+$rem;
        } 
        return $sum; 
    } 
      
    // Driver code 
    $num = 123456; 
    echo "Sum Of Digits : "
            . sum_digit($num); 
    
    #include <stdio.h>
    int sum_digit(long long num) 
    { 
        int sum = 0,rem; 
        while (num != 0) { 
            rem=num%10;
            num = num / 10; 
            sum=sum+rem; 
        } 
        return sum; 
    } 
      
    // Driver code 
    int main(void) 
    { 
        long long num = 123456; 
        printf("Sum Of Digits : %d", 
               sum_digit(num)); 
        return 0; 
    } 
    

    Output

    Sum Of Digits : 21
    

    Recommended Programs

       Program to count the number of digits in a number.
       Program to find perfect number.