Sum of Digits of a Five Digit Number in C - Hacker Rank Solution.




 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 it 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

Output Format

Print the sum of the digits of the five digit number.

Sample Input 0

10564

Sample Output 0

16
Solution : -
            #include <stdio.h>
            #include <string.h>
            #include <math.h>
            #include <stdlib.h>
int main() {
    
     int number,sum=0,reminder;
     scanf("%d", &number);
     while(number>0)    
{    
reminder=number%10;    
sum=sum+reminder;    
number=number/10;    
}    
printf("%d",sum);    
return 0;  
}   
  

Popular posts from this blog

When a method in a subclass overrides a method in superclass, it is still possible to call the overridden method using super keyword - Hacker Rank Solution.

Java's System.out.printf function can be used to print formatted output. The purpose of this exercise is to test your understanding of formatting output using printf. To get you started, a portion of the solution is provided for you in the editor; you must format and print the input to complete the solution.