Click here to Skip to main content
15,884,960 members
Please Sign up or sign in to vote.
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 5:08am
v3

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.
 
Share this answer
 
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);
}
 
Share this answer
 
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;
}
 
Share this answer
 
Comments
Patrice T 27-Jan-22 3:13am    
+5
CPallini 27-Jan-22 4:16am    
Thank you!
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.
 
Share this answer
 
Comments
Member 15514081 26-Jan-22 23:30pm    
It just returns for 4 as answer instead of 16. The integer is 10564.
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;
}
 
Share this answer
 
v2
Comments
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.
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;
    printf("Enter your number:");
    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:
Enter your number:10564
Sum of its Digits: 16
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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