Home » » Downward Triangle

Downward Triangle

Written By Unknown on Sabtu, 21 Januari 2012 | 23.27

Lab RPL .
Just like the previous example, the program will involve nested loops to print out a downward facing triangle. The triangle will go from the top right to the bottom left,
//for loop for downward triangle
#include <iostream>
void main ()
int size;
cout<<" How Large a triangle do you want? ";
if (size <0 | |size > 10) exit (1);
for (int r = 0;r<size ; r++){
  for (int c=1; c<=size ; c++) {
   if (c<=r) cout<<" ";
else cout<<"*";
return 0

Most of this program is like the previous example however the logic is a bit different. inside the column loop here, the if statment checks that if the column is less than the row, print out a white space, otherwise print out star. Why is this ? it is so because in order for the tringle to be printed out correctly. there must be white spaces Before the stars to keep the order correct.
As an example, say the user enteres the number 6.
Here is what this triangle would look like ;

0 komentar:

Posting Komentar