Sum of Digits of a Five Digit Number - Hackerrank solution

 

Sum Of Digits Of a Five Digit Number


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.


Solution:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main(){
    int n, t, sum = 0, remainder;
   scanf("%d", &n);

   t = n;

   while (t != 0)
   {
      remainder = t % 10;
      sum       = sum + remainder;
      t         = t / 10;
   }

   printf("%d\n",sum);

   return 0;
}

Note:

This Code is Verified by all Test Cases.If any error occurs then Comment correct code Below in comment box.

Disclaimer:-

 The above all problem statement is given by hackerrank.com, but the solution is generated by the CodexRitik . if any of the query regarding this post or website fill the following contact form Thank You. 


Sum of Digits of a Five Digit Number - Hackerrank solution Sum of Digits of a Five Digit Number - Hackerrank solution Reviewed by CodexRitik on March 15, 2020 Rating: 5

No comments:

Powered by Blogger.