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


Please Help Members By Posting Answers For Below Questions

What is meant by realloc()?

668


Explain what is wrong with this program statement?

616


What's the difference between constant char *p and char * constant p?

649


How can you pass an array to a function by value?

597


Explain how can you determine the size of an allocated portion of memory?

616






How do you construct an increment statement or decrement statement in C?

735


How can I avoid the abort, retry, fail messages?

651


Explain why c is faster than c++?

566


What is the difference between variable declaration and variable definition in c?

557


Can you please compare array with pointer?

611


Does c have function or method?

583


When a c file is executed there are many files that are automatically opened what are they files?

587


Array is an lvalue or not?

631


Explain spaghetti programming?

680


Can you apply link and association interchangeably?

665