Best Answer

int sumDigits(int n) {

int sum = 0;

while( n > 0 ) {

sum += (n % 10); // add last digit of n to sum

n /= 10; // divide n by 10 to "chop off" the last digit

}

return sum;

}

____________________________________________________

C program to find the sum of entered digit: By Jatinder Pal Singh#include

#include

void main()

{

clrscr();

int n,num,x,sum=0;

printf("Enter a number=");

scanf("%d",&n);

while(n>0)

{

x=n%10;

sum=sum+x;

n=n/10;

}

printf("Sum of digits of a number=%d",sum);

getch();

}

Study guides

☆

Q: Write a c program to compute the sum of digits of a given integer number?

Write your answer...

Submit

Still have questions?

Related questions

the sum of individual digits of a given integer number

Any number that has non-zero digits after the decimal point is NOT an integer.

see the program

To count the number of times a digit occurs in an integer, start by initializing an array of ten counts of digits, such as int digits[10];Then, in a loop while the number is non zero, increment the element in the digits array that corresponds to the units digit, and then divide the number by ten, such as digits[number%10]++ and number/=10;int digits[10];int i;int number = some number;for (i=0; i

Any number that has non-zero digits after the decimal point is NOT an integer.

Any number with non-zero digits after the decimal point is NOT an integer.

Basically, an integer is a number that does NOT have digits after the decimal point (nor should it have a fractional part).

No; an integer is a number without decimal digits, or without a fractional part.

No integer fits those conditions.

Such a method could be: public int NumberOfDigits(int num) { num = num/10; if( num == 0) return 1; return ( 1+NumberOfDigits(num) ); }

Basically a number without decimals (digits after a decimal point). this can be either positive or negative.

A number with more than one digits: that is, an integer greater than 9.

People also asked