Sum of Digits of a Five Digit Number|||| hackerrank || C language || programming_info

Objective

In order to get the last digit of a number, we use modulo operator \%. When the number is modulo divided by 10 we get the last digit.

To find first digit of a number we divide the given number by  until number is greater than . At the end we are left with the first digit.

Task

In this challenge, you have to input a five digit number and print the sum of digits of the number.

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
Now here's the solution in C:-

code snippet:
#include <stdio.h>
#include <math.h>

int main() {
    int n,s=0;
    scanf("%d", &n);
    while(n!=0)
    {
    s=s+n%10;
    n=n/10;
    }
    printf("%d\n",s);
    return 0;
}

Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support