Question

Write a C program program to calculate the sum of digits of a number: Use modulus...

Write a C program program to calculate the sum of digits of a number: Use modulus
operator (%) to extract individual digits of a number and keep on adding them.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

int main() {
    int n;
    printf("Enter a positive integer: ");
    scanf("%d", &n);
    int sum = 0;
    while(n > 0) {
        sum += n%10;
        n /= 10;
    }
    printf("Sum of all digits = %d\n", sum);
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Write a C program program to calculate the sum of digits of a number: Use modulus...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT