if a five digit number is input through the keyboard, write
a program to calculate the sum of its digits.
(hint:-use the modulus operator.'%')
Answer Posted / thamizharasan
#include"stdio.h"
main ()
{
int number, last_digit, next_digit, total;
printf ("Enter the number whose sum of digits is to be calculated: ");
scanf ("%d", &number);
last_digit = number%10;
total = last_digit;
next_digit = (number/10) % 10;
total = total + next_digit;
next_digit = (number/100) % 10;
total = total + next_digit;
next_digit = (number/1000) %10;
total = total + next_digit;
next_digit = (number/10000) %10;
total = total + next_digit;
printf ("The sum of the digits of the entered number is: %d", total);
}
Is This Answer Correct ? | 3 Yes | 4 No |
Post New Answer View All Answers
What is meant by realloc()?
Explain what is wrong with this program statement?
What's the difference between constant char *p and char * constant p?
How can you pass an array to a function by value?
Explain how can you determine the size of an allocated portion of memory?
How do you construct an increment statement or decrement statement in C?
How can I avoid the abort, retry, fail messages?
Explain why c is faster than c++?
What is the difference between variable declaration and variable definition in c?
Can you please compare array with pointer?
Does c have function or method?
When a c file is executed there are many files that are automatically opened what are they files?
Array is an lvalue or not?
Explain spaghetti programming?
Can you apply link and association interchangeably?