Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Sum of Digits of a Five Digit Number solution in c

YASH PAL, 15 July 20243 October 2025

HackerRank Sum of Digits of a Five Digit Number solution in C – In this tutorial we are going to solve the HackerRank Sum of Digits of a five-digit number problem or write a program for that. In this problem we need to take an input of a five-digit number that is always greater than 10000 and less than 99999. and on the output screen, we need to print the sum of that 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 10 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, n.

Constraints

10000 <= n <= 99999

Output Format

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

Logic – How are we going to calculate the sum?

Let say we have a digit 45644 and if we module this number by 10 then we get the last digit of the number that 4 means 45644%10 then the remainder is 4. After the division by 10 45644/10 we get the remaining 4 digits 4564. we will use this logic five times and will sum the reminder and we will get the sum of 5 digit number. Similarly, we can apply this logic to any length of number.

HackerRank Sum of Digits of a five digit number solution in c
HackerRank Sum of Digits of a five digit number solution in c

Sum of Digits of five digit number solution in C

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

int main() {
	
    int n;
scanf("%d", &n);
int digit, temp, sum = 0;
temp = n;
//Complete the code to calculate the sum of the five digits on n.
while(temp > 0)
{
    digit = temp % 10;
    sum = sum + digit;
    temp = temp / 10;
}
printf("%d\n",sum);
return 0;
}
HackerRank Sum of Digits of a Five Digit Number solution in C

Next problem solution – HackerRank Bitwise operators in C Solution

C Solutions coding problems solutions Hackerrank Problems Solutions cHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes