sum of the digits of the 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,
Constraints
10000<n<99999
Output Format
Print the sum of the digits of the five digit number.
SOLUTION :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,temp,sum=0,last_num;
scanf("%d", &n);
temp = n;
while(temp > 0)
{
last_num = temp %10;
sum = sum + last_num;
temp = temp/10;
}
printf("%d",sum);
return 0;
}
INPUT:
10564
OUTPUT:
16
I am so appreciative of your blog. That is a very valuable article for me. Thanks for sharing this article. Thanks for sharing your ideas, this is appreciable. I like the given information. Keep Updating…
ردحذفFor More : What is computer? Different Types of Computers
إرسال تعليق