| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need help with C++ program
I am writing this program, and I would like to know if the first part of it is correct, and if it is, I will be able to figure out the rest of the program. I know this is a "do while loop," I just want to be sure that I am doing it the right way. Here is the program.
#include <iostream.h> void main() { int X, Y; X = 36; Y = 20: while (X > Y) { X = X - Y; Y = Y/2; } cout<<"X=" <<X<<" and Y =" <<Y<<end1; } ======================================== / HERE IS MY ANSWER /======================================== #include <iostream.h> void main() { int X, Y; X = 36; Y = 20: while (X > Y) { X = 16; Y = 8; } cout<<"X=" <<X<<" and Y =" <<Y<<end1; } Any help you can give me will be greatly appreciated. |
|
#2
|
||||
|
||||
|
I'm not sure if I understand your question correctly. The code at the top is a valid program, that will run a certain calculation untol X is smaller than Y.
The second code is not the first iteration of the loop in the program! You start out with X = 36, Y = 20 After one loop: X = 16 (36 - 20) and Y = 10 (20 / 2) after second loop: X = 6 (16 - 10) and Y = 5 (10 / 2) after third loop: X - 1 (6 - 5) and Y = 2 (10 / 2, integer style (rounded down)) there is no fourth loop, since X is now smaller than Y. So output will be Code:
X=1 and Y =2
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 278
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Need help with C++ program |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|