Sum of Digits of a Five Digit Number
Objective
The modulo operator,%, returns the remainder of a division. For example, 4 % 3 = 1
and 12 % 10 = 2
. The ordinary division operator, /
, returns a truncated integer value when performed on integers. For example, 5 / 3 = 1
. To get the last digit of a number in base 10, use as the modulo divisor.
Task
Given a five-digit integer, print the sum of its digits.
Input Format
The input contains a single five digit number, .
code-
#include <stdio.h>
int main()
{
int num;
int digit;
int sum=0;
scanf("%d", &num);
while(num>0)
{
digit = num % 10;
sum = sum+digit;
num = num / 10;
}
printf("%d",sum);
return 0;
}
Comments
Post a Comment