Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
void main ()
{
int digit;
for (digit=0;digit<=9;digit++)
cout<<digit<<"\n";
digit=5*digit;
--digit;
cout<<digit;
}


What I have tried:

void main ()
{
int digit;
for (digit=0;digit<=9;digit++)
cout<<digit<<"\n";
digit=5*digit;
--digit;
cout<<digit;
}
/* i dont know the 49 how its comes */
Posted
Updated 11-Jan-17 11:02am
Comments
[no name] 11-Jan-17 10:33am    
Use the debugger to step through your code.

If you know the basics of how C programs work, then you can easily map the value to the output of the program,
C++
// Write clean code, it makes more sense.
void main ()
{
    int digit;                                    // create a new variables
    for (digit = 0; digit <= 9 ; digit++) {       // run the loop using "digit" variable
                                                  // until digit becomes 10
        cout << digit << "\n";                    // print current digit, each time
    }

    digit= 5 * digit;                             // Multiply last value of digit (10)
                                                  // with 5
    --digit;                                      // Decrease its value by 1 (--)
    cout << digit;                                  // Print it.
}

And we all know that 5x10=50 and 50-1=49. If this program doesn't make sense, please ask your instructor to start again and teach these to you again as you have no idea about them at all. This is a common problem among beginners, they don't understand the basics and they try to build something big — which does not work.
 
Share this answer
 
Comments
hassan mci 12-Jan-17 7:23am    
digit<=9 ??? it never runs till 10 the loop runs tills 9 . so 9*5 = 45 and than -- so 44 .but the output comes 49 . Any ways thanks very much you give me suggestion
Member 12173907 12-Jan-17 13:38pm    
The condition becomes wrong when the value of the digit is 10 because 10<=9 is not correct. So the loop terminates leaving the value of digit to 10

Please run a debugger to understand this yourself.
What about discovering by yourself what the code is doing with the help of debugger ?

You should learn to use the debugger as soon as possible, it is an incredible learning tool. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

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 find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2

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