C++: Learning the Basics – Playing around with the WHILE LOOP.

In the last post, I had left you with a challenge: user enters ten numbers and the program displays the largest. Here is the code for the program:

Situation 4: User enters ten numbers and the program displays the largest of the lot.

#include <iostream.h>
#include <conio.h>
void main()

{ 
  clrscr(); 
  int large=0, i=0, num;
 
  cout<<"\n\t Enter a number: "; 
  cin>>large;
 
  while (i<9) 
  {
    cout<<"\n\t Enter another number: "; 
    cin>>num;
 
    if (num>large) 
    { 
       large=num; 
    } 
    i++; 
  }
 
  cout<<"\n\t\ Enter largest number: "<<large; 
  getch; 

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s