Page 64 - Modular_V2.0_C++_Flikpbook
P. 64

Program 4: To input any number and print it in reverse order using while loop. The input number
                  must be positive.

                  #include<iostream.h>
                  #include<conio.h>
                                                            DOSBox 0.74, Cpu speed: max 100% cycles, Frames 0, Program:
                  void main()
                                                        Enter a positive number to be reversed: -987
                  {
                                                        The number must be positive. Please enter again:
                      clrscr();
                      long int n, digit;

                      cout<< "Enter a positive number to be reversed: ";
                      cin>> n;                              DOSBox 0.74, Cpu speed: max 100% cycles, Frames 0, Program:
                      while(n <= 0)                     Enter a positive number to be reversed: 12345
                                                        The reverse of the number 12345 is: 54321_
                      {
                          cout<< "The number must be positive. Please enter again: ";

                          cin>> n;
                      }
                      cout<< "The reverse of the number " << n << " is: ";
                      while(n > 0)
                      {

                          digit = n % 10;
                          cout<< digit;
                          n = n / 10;
                      }
                      getch();

                  }
                                 Tech Funda



                         If you want to execute just one statement in the body of the while loop, you do not
                         have to enclose the statement in braces.



                     THE DO-WHILE LOOP

                  The do-while loop is similar to the while loop. The only difference is that, in a do-while loop,

                  the condition  is stated at the end of the loop. This ensures that the body of the loop executes at
                  least once. The syntax of the do-while loop is:
                       do
                       {

                           statements;
                       }
                        while(condition);

                   62
                          Touchpad MODULAR (Ver. 2.0)
   59   60   61   62   63   64   65   66   67   68   69