Assignment 3
Assignment 3
Assignment No. 03
LESSON 5 A
LAB 5.1: Working with the ‘While Loop’
Lab 5.1(a):
#include <iostream>
using namespace std;
int main()
{
char letter = 'a';
while (letter != 'x')
{
cout << "Please enter a letter" << endl;
cin >> letter;
cout << "The letter you entered is " << letter << endl;
}
return 0;
}
Lab 5.1(b):
#include <iostream>
using namespace std;
int main()
{
int month = 1;
float total = 0, rain;
cout << "Enter the total rainfall for month "
<< month << endl;
cout << "Enter -1 when you are finished" << endl;
cin >> rain;
while (rain != -1)
{
total += rain;
month++;
cout << "Enter the total rainfall in inches for month "
<< month << endl;
cout << "Enter -1 when you are finished" << endl;
cin >> rain;
}
if (month == 1)
cout << "No data has been entered" << endl;
else
cout << "The total rainfall for the "
<< month - 1
<< " months is " << total << " inches." << endl;
return 0;
}