15,504,424 members
1.00/5 (1 vote)
See more:
Given a five digit integer, print the sum of its digits.

Constraints 10000<= n >= 99999
Given integer = 10564

The answer I'm supposed to get is 16.

What I have tried:

C
```int main() {

int n, s =0;
scanf("%d", &n);
while(n >= 10000 && n <= 99999)
{
s += n%10;

}

printf("%d", s);
return 0;
}```
Posted
Updated 27-Jan-22 6:08am
v3

## Solution 3

Quote:
What am I doing wrong.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

## Solution 5

Just o keep your interest moving, there is a recursive way of doing it. You can add your if conditions where necessary

C++
```int SumDigit(int number)
{
int digit = number % 10;
number = number / 10;
if (number <= 0)
return digit;
return digit + SumDigit(number);
}```

## Solution 6

Try
C
```#include <stdio.h>

int main()
{
int n, s;
scanf("%d", &n);

if ( n < 10000 || n > 99999) return -1;

for ( s = 0; n; n /= 10)
s += n % 10;

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

return 0;
}```

Patrice T 27-Jan-22 3:13am
+5
CPallini 27-Jan-22 4:16am
Thank you!

## Solution 1

You are adding `n` modulo `10` to `s` each time through your loop. But once you've done that, you need to do `n /= 10` (divide `n` by `10` and throw away the remainder). As it is, `n` will never get smaller, and the code will run forever.

Member 15514081 26-Jan-22 23:30pm
It just returns for 4 as answer instead of 16. The integer is 10564.

## Solution 2

C++
```int main() {

int n, s =0;
scanf("%d", &n);

if (n < 10000 || n > 99999) {
printf("Wrong Input, number should be 5 digit");
return 1;
}

while(n >= 1 && n <= 99999)
{
s += n%10;
n /= 10;
}

printf("%d", s);
return 0;
}```

v2
Member 15514081 26-Jan-22 23:55pm
Look into the constraints. I am getting the right answer when I start it from 1 but the constraint is that I am always above 10000. What does this mean?
Manish K. Agarwal 28-Jan-22 3:13am
you mentioned about 5 digits, so corrected the condition to accept only 5 digits number.

## Solution 4

You need to introduce an aditional flag to control the constratint. See below code:
```// Online C compiler to run C program online
#include <stdio.h>

int main() {

int n, inputnumber, s =0;
scanf("%d", &n);
if(n < 10000 || n > 99999)
{
printf("Invalid Number: Input Range is Between (10000 - 99999)");
}
inputnumber = n;
while(inputnumber >= 10000 && inputnumber <= 99999 && n != 0)
{
s += n%10;
n = n/10;
}
if(s > 0)
printf("Sum of its Digits: %d", s);
return 0;
}```

Output:
Sum of its Digits: 16

Top Experts
Last 24hrsThis month
 Richard Deeming 128 OriginalGriff 123 CPallini 105 Richard MacCutchan 60 longjmp 35
 Richard Deeming 128 OriginalGriff 113 CPallini 105 Richard MacCutchan 60 CHill60 25

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900