# Thread: Output issues

1. ## Output issues

Hey guys, I wanted to create a program that will read an integer and display the sum of the digits of the integer, if the sum of the digits of the integer is more than 9 then the sum of the digits of that number will be calculated as well. This is done until the answer becomes an integer that is between 1 to 9. Don't mind the mechanism if it is confusing to understand, the problem is with the last if statement, I cannot seem to get the right output.

Code:
```#include <stdio.h>
int main()
{
int number;
int remainder, remainder2, remainder3, sum=0, sum2=0, sum3=0;

scanf("%d", &number);

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

if(sum>9)
{
remainder2=sum%10;
sum2=sum2+remainder2;
sum=sum/10;
sum2=remainder2+sum;
}
if(sum2>9){
remainder3=sum2%10;
sum3=sum3+remainder3;
sum2=sum2/10;
sum3=remainder3+sum2;
}

if(sum<=9)
printf("%d \n", sum);
if else(sum2>9)
printf("%d \n", sum2);
else
printf("%d \n", sum3);

}```
Specifically the wrong part of the code is
Code:
` if else(sum2>9)`
Printing out all the different sums works as I want it to, the If statement logic does not work at all in the part mentioned above.

I really appreciate your help guys, this problem has been a pain in the ass for the past 3 days.

2. Use "else if" rather than "if else".

The problem is a coding issue, not an output issue.

3. Hey there, thanks for your reply!

I tried switching it to if else and when I enter 399 into the output the result should be a 3, but it still displays a 2. Any ideas?

Can you try to show me your fixed version of the code, if you don't mind?

4. Yes, I do mind. This is not a free code factory.

You're the one who said not to worry about the mechanism of the code. I simply pointed out a problem that would prevent your code compiling. Code that can't be compiled can never be executed.

Given that I don't know what the code is trying to achieve, I am certainly not attempting to fix it.

All I'll do is point out why input of 399 gives output of 2.

1) After the first loop, sum will contain the value 21 (3 + 9 + 9).

2) Hence the first "if(sum > 9)" tests true. The code executed if (sum > 9) computes remainder2 as 1, sum2 as 1, sum as 2, and sum2 as 3.

3) The next "if (sum2 > 9)" is false, since sum2 is 3.

So the values we have for sum, sum2, and sum3 are 2, 1, and 3 respectively.

The next test "if (sum <= 9)" is true so the value 2 is output. Given the nature of if/else the other conditions are not checked. Done and dusted.

As to what the code should do, I don't know. Look at the above description of what your code DOES, identify how that is different from what you intend, and change the code as needed to correct that.

You might want to learn to use a debugger - a program that allows you to step through your code one step at a time. Then you will see that your code does what I have described above.

5. I got my code to work without your help, who's laughing now mr. Code factory?!

6. Originally Posted by Ashley95
I got my code to work without your help, who's laughing now mr.
I am, since I got you to learn something by solving your own problem. Which was my primary objective in posting.

7. Originally Posted by Ashley95
Hey guys, I wanted to create a program that will read an integer and display the sum of the digits of the integer, if the sum of the digits of the integer is more than 9 then the sum of the digits of that number will be calculated as well. This is done until the answer becomes an integer that is between 1 to 9. Don't mind the mechanism if it is confusing to understand, the problem is with the last if statement, I cannot seem to get the right output.

Code:
```#include <stdio.h>
int main()
{
int number;
int remainder, remainder2, remainder3, sum=0, sum2=0, sum3=0;

scanf("%d", &number);

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

if(sum>9)
{
remainder2=sum%10;
sum2=sum2+remainder2;
sum=sum/10;
sum2=remainder2+sum;
}
if(sum2>9){
remainder3=sum2%10;
sum3=sum3+remainder3;
sum2=sum2/10;
sum3=remainder3+sum2;
}

if(sum<=9)
printf("%d \n", sum);
if else(sum2>9)
printf("%d \n", sum2);
else
printf("%d \n", sum3);

}```
Specifically the wrong part of the code is
Code:
` if else(sum2>9)`
Printing out all the different sums works as I want it to, the If statement logic does not work at all in the part mentioned above.

I really appreciate your help guys, this problem has been a pain in the ass for the past 3 days.
this type of problems are better to solve using dynamic programming technique, it'll also optimize your code in terms of time, try it if you can.

Popular pages Recent additions