-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Tuesday, 28 April 2015

Making an inverted right-angled triangle

Hello and Asalam o Alikum.I hope you all are fine by the grace of Allah Almighty.
Today I am here with a very famous program of nested loops.

You have often made an inverted right-angled triangle on your notebook or register but today I will show you how to make it in C++. Its coding is very simple and interesting

As you know that the syntax of nested loops is

Syntax

for(initialization;condition;increment/decrement )//outer loop
{
for(initialization;condition;increment/decrement)//inner loop
statement(s)
}

Below is the source code of this program.

Source code


Output


How the program works?

The outer loop of this program is from 0-5. It starts its iteration when the value is 1.
Then it jumps to the inner loop whose loop is from "i"  to 5.So, the value of loop will be from 1-5.As a result 5 stars will be printed.

Again it jumps to the outer loop where the value now becomes 2.Then it jumps to the inner loop and the same process is repeated.This time 4 stars are displayed on the next line.

This process is repeated again and again until the value of outer loop becomes 6 which is against the given condition. So ,the loop is terminated.

No comments:

Post a Comment