Pages

Sunday, September 01, 2013

Write a program of A while loop to print number in C++

Simple while loop that prints numbers 10 per line

Source Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
   #include <iostream.h>
   #include <conio.h>
   #include <iomanip.h>
   int main ()
     {
      cout << " Enter an integer between 1 and 100:  ";
      int num;
      cin  >> num;
      
   // Test number
   if (num > 100)
       num = 100;
   int lineCount = 0;
   while (num > 0)
    {
      if (lineCount < 10)
          lineCount++;
      else
      {
          cout << endl;
          lineCount = 1;
          }// else
   cout << setw(4) << num--;
        } //while
        getch ();
        return 0;
               } // main  

Result:


 Enter an integer between 1 and 100:  15
  15  14  13  12  11  10   9   8   7   6
   5   4   3   2   1 

No comments:

Post a Comment