answersLogoWhite

0


Best Answer

#include<iostream>

int sum_digits(int num, int base=10)

{

int sum=0;

while( num )

{

sum+=num%base;

num/=base;

}

return(sum);

}

int main()

{

int sum=sum_digits(42);

// assert( sum==6);

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

#include<stdio.h>

int sumOfDigit(int x){

int ans = 0;

while(x>0){

ans += (x%10);

x /= 10;

}

return ans;

}

int main(void){

printf("%d",sumOfDigit(12));

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

if we have five digits then this may be the solution

#include <stdio.h>

int main(void)

{

int i,temp = 0;

printf("Enter a +ve 5 digit number :");

scanf("%d",&i);

if (( i <= 99999 ) && ( i > 9999 ))

{

printf(" %d",(i/10000) + 1);

printf(" %d",((( i/100) % 100)/10) + 1);

printf(" %d",(( i/100) % 10 ) + 1);

printf(" %d",((i % 100) / 10) + 1 );

printf(" %d",(i % 10) + 1);

}

else

printf("\n Pl.enter +ve five digit number");

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

int main() {

int num, sum;

printf ("Enter a positive integer: ");

scanf ("%d", num);

sum = 0;

while (num>0) {

sum += num%10;

num/=10;

}

printf ("The sum of the digits is: %d\n", sum);

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

for (digsum= 0; n!=0; digsum+=n%10, n/=10);

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c program to find the sum of the digits of a number?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a Shell program to find the sum of cube of individual digits of a number?

no thanks


How to write A Java program to print number of digits in a given number?

One way to do this is to convert the number to a String, then use the corresponding String method to find out the length of the String.


Write a program to find the number of digits in the number?

#include &lt;stdio.h&gt; int main(int argc, char **argv) { if (argc&lt;1) { printf("Usage: %s number\n",argv[0]); return -1; } int digits=1, i=atoi(argv[1]); while (i/=10) ++digits; printf("%d\n",digits); }


Shell program to find the sum of square of individual digits of a number?

There are many shell programs that will find the sum of the square of individual digits of a number. A small example is: SD=3n=2, sum=2, and SD=2.


Write a pseudo code to find the sum of digits of a given number reducing them to a single digit?

5


Write a Shell program to find the smallest number from a set of numbers?

k


Write a java program to find out the sum of a number of n digits?

class Sum_Of_Digits { public static void printSumandnoofdigits(int n) { int temp = n; int count = 0; int sum = 0; while ( n &gt; 0 ) { sum = sum + n % 10; n = n / 10; count ++; } System.out.println("The number is..." + temp ); System.out.println("The sum of digits is..." + sum); System.out.println("The number of digits is..." + count); } }


Shell program to find the sum of cube of individual digits of a number?

Shell problems are programs that can be run to find out information about numbers. The problem can help find an even or odd number, or what the sum of a cube is.


How do you write a java program to find the square root of a number?

You can use the Math.sqrt() method.


Write a c program to find given number is prime or not?

Yes, do write, or if you're too lazy to your homework, use google.


What is the largest prime no that is stored in 8 bit pattern?

Write your own prime number program and find out.


How do you find the sum of digits of each number?

Add the digits together. The sum of the digits of 23 is 5.