C Program to find sum of all digits in a number is a simple to program.
To calculate the sum of digits first we need to separate out each digit in number then add digits in Number. We use the %( modulus) operator to separate out digits in the number.

Algorithm to find sum of all digits in number

  1. Take the input number. No
  2. While no > 0
    Separate out digit digit =no%10;
    Calculate sum= sum + digit
  3. Remove the first digit from a number or to get remaining number no = no/10;
  4. Print Sum of digits in a number

C Program to find sum of all digits in number

#include<stdio.h>

void main()

{

int dummy,n,sum=0,x;

printf("Enter a number\n");

scanf("%d",&n);

dummy=n;

while(n>0)

{

x=n%10;

sum=sum+x;

n=n/10;

}

printf("The sum of all digits in %d is %d\n",dummy,sum);

}

Output :

C Program to find sum of all digits in number

Write A Comment