Lab 9 - Loops
Lab 9 - Loops
Practice Codes:
#include <iostream>
using namespace std;
int main()
{
int count; // control variable also used after loop terminates
for ( count = 1; count <= 10; count++ ) // loop 10 times
{
cout << count << " ";
} // end for
cout << "\nBroke out of loop at count = " << count << endl;
return 0;
} // end main
#include <iostream>
using namespace std;
int main()
{
for ( int count = 1; count <= 10; count++ ) // loop 10 times
{
if ( count == 5 ) // if count is 5,
cout << count << " ";
} // end for
1. Practice the examples, discuss in theory lecture and from the Chapter # 5 of text book.
2. Write a C++ program to enter numbers until the user wants to stop and at the end it should display the
count of positive, negative, even, odd and zeros entered.
3. Write a C++ program to display a table showing value of x, the square of x, and the cube of x
from 1 to 50.
4. Use nested loops to print numbers such that the outer loop prints the number of rows and the
inner loop prints the number of columns. With each increasing row, the number of columns
increases. The output looks as follows.
1
12
123
1234
12345
123456
1234567
12345678
123456789
5. Write a program that uses for statements to print the following patterns separately, one below
the other. Use for loops to generate the patterns.
6. (Diamond of Asterisks) Write a program that prints the following diamond shape. You may
use output statements that print a single asterisk (*), a single blank or a single newline.
Maximize your use of repetition (with nested for statements) and minimize the number of
output statements.