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
Why c is called procedure oriented language?
is it possible to create your own header files?
int main() { Int n=20,i; For(i=0;i<=n;i--) { Printf(“-“); Return 0;
Why is c called a mid-level programming language?
How can I do graphics in c?
Is c call by value?
What is the advantage of a random access file?
How can I change their mode to binary?
What is a program?
Why doesnt this code work?
What is c language used for?
Explain what is the difference between far and near ?
What is c basic?
Tell me the use of bit field in c language?
What is the ANSI C Standard?