Like Tree7Likes
  • 1 Post By grumpy
  • 6 Post By grumpy

Output issues

This is a discussion on Output issues within the C Programming forums, part of the General Programming Boards category; Hey guys, I wanted to create a program that will read an integer and display the sum of the digits ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    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. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,429
    Use "else if" rather than "if else".

    The problem is a coding issue, not an output issue.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    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. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,429
    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.
    AndiPersti likes this.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    I got my code to work without your help, who's laughing now mr. Code factory?!

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,429
    Quote Originally Posted by Ashley95 View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    24
    Quote Originally Posted by Ashley95 View Post
    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 subscribe to a feed

Similar Threads

  1. C++ overlapping output and adding extensions to output files
    By lordmorgul in forum Linux Programming
    Replies: 9
    Last Post: 05-11-2010, 08:26 AM
  2. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 09:46 PM
  3. Printing to file / Output issues
    By Roflcopter in forum C++ Programming
    Replies: 19
    Last Post: 10-11-2007, 07:38 PM
  4. Replies: 3
    Last Post: 02-19-2003, 07:34 PM
  5. Output to screen display issues
    By spazjr01 in forum C++ Programming
    Replies: 3
    Last Post: 12-16-2002, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21