Home » » Forward Triangle

Forward Triangle

Written By Unknown on Sabtu, 21 Januari 2012 | 23.02

Lab RPL .
The objective of this program is to get used to nestet looping.
This program will print out a triangle of stars using two loops, one nested inside the other. It will ask a use for an positive input less than or equal to 10.

//for loop for forward triangle
#include <iostream>
void main ()
int size ;
cout<< How Large a triangel do you want ? " ;
cin>>size ;
if (size < 0 | | size > 10 ) exit (1);
for (int r = 1 ; r <= 10 ; r++) {
  for (int c+ 1; c<= r; c++)
     cout<<"*" ;
cout<<endl;
}
return 0;
}

when the user enters a correct number , a triangle is printed that will go from the top left to the bottom right. Let's see what this program does with the nested loop.
The "row" loop is first followeb by the "column" loop. The rule is , the inside loop(s) will exercise fully, followed by the one above it,then above that etc. . .  so here, the row loop will have a value of 1 to begin and the column loop will also have a value of 1. but each time the column loop changes, the value of the row Does Not change, until the column loop is completely finished.
Say that the user enterers the number 6 .
Here is what this triangle would look like :

*
**
***
****
*****
******

0 komentar:

Posting Komentar