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



Sum of digits :- Here we will discuss how we can find the sum of digits in a given number. Let us understand that with the help of an example.

For Example :-
Input:- 12548
Output:- 20

Explanation:- As you can see the given number is 12548 and we have to calculate the sum of digits in the given number, so here we have the following digits 1, 2, 5, 4, 8 and we will sum all these digits -> 1 + 2 + 5 + 4 + 8 and the result we get is 20 that will be our output.

Below you will find its algorithm and program.

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.