-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;
Showing posts with label Nested loop. Show all posts
Showing posts with label Nested loop. Show all posts

Tuesday, 14 July 2015

Making a right-angled triangle in C++

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 a 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

So,have a look at its source code.

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 1 to outer loop.As the value of loop is one, so 1 star will be displayed.

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 two 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.

Tuesday, 5 May 2015

Making a multiplication table

Hello and Asalam o Alikum. I have already discussed the concept of  "nested loops" in my blogs.

As you know that a loop within another loop is known as nested loop. The outer loop is normally used for rows and the inner loop is used for columns.

So,today I am here with a very interesting program. This program is about making a multiplication table using nested loops.

Below is the source code of this program.

Source code


Output

Program's logic

1*1     1*2    1*3    1*4    1*5    1*6   1*7     1*8   1*9     1*10

2*1      2*2    2*3     2*4    2*5    2*6    2*7    2*8    2*9  2*10

3*1    3*2      3*3     3*4     3*5   3*6    3*7   3*8   3*9   3*10

4*1    4*2      4*3     4*4     4*5    4*6    4*7    4*8   4*9    4*10

5*1    5*2     5*3      5*4     5*5    5*6    5*7     5*8    5*9    5*10

Sunday, 3 May 2015

Making a diamond in C++

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.

Today I am here with a source code of making a diamond in C++.

So, don't be afraid !!! Making a diamond is not a difficult task anymore !!!

I will use 6 loops for this purpose. The division of these loops are as below:

3 loops for pyramid.

3 loops for inverted pyramid.

so, it means that

Diamond=pyramid+inverted pyramid. If you know how to make a pyramid and inverted pyramid,

you can easily make a diamond.

So,below is the source code of this program.

Source code


Output


How the program works?

The first 3 loops in the source code are used for pyramid and last 3 loops are used for inverted pyramid(the logic of making a pyramid is already described in my blog).


Thursday, 30 April 2015

Making a Pyramid

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.

Today I am here with a new blog that will help you to make a pyramid in C++.

What is a pyramid?

The above figure is actually called a pyramid.
After reading this blog, you would be able to draw a pyramid very easily inshallah.So,below is the source code of this program.

Source code



Output


How the program works?


In this program I have used 3 loops. The “s” loop is actually used for spaces. So, according to the given condition, the value of loop “i” is from 0-10.It is actually the outer loop. As the condition is true, so it will move towards the next loop, “s”.

 The value of “s” loop is from i-10.It is used for printing spaces. In the first iteration, it will print 11 spaces(and in the next iteration,it will print 10 spaces). After printing spaces, it will move towards next loop that is used to print the stars.

The value of “j” loop is from 0 - i*2. It means that it will display only 1 star in the beginning.

Now, in the next iteration, it will display 3 stars as the value of j is from 0 - 1*2(note that the value of i is 1 in this condition).


This process continues again and again and as a result, a pyramid is displayed before the user.



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.

Thursday, 23 April 2015

Nested loops in C++

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.

Today I here with one of the most important topics of C++,"Nested loops".

Nested Loops

A loop within another loop is known as nested loop or the body of a loop can itself contain a loop which is known as nested loop.
In nested loop, the inner loop is executed only when the outer loop is executed. If the condition in the outer loop becomes false, the inner loop will not execute at all.

Syntax

for(initialization;condition;increment/decrement )//outer loop
{
for(initialization;condition;increment/decrement)//inner loop
statement(s)
}
 
The source code of a simple "nested loop" program is given below:

Source code



Output


Explanation:

                      In the above example, the iteration for outer loop is from 0-7  and the iteration for inner loop is also from 0-7.

How the program works?

The value of outer loop is 1 in the start. As it is less than 7,so the condition becomes true. It will immediately moves towards the inner loop. The iteration in inner loop us from 0-7. So, it will display "01234567".It will again moves towards the outer loop.

Next time, the value in the outer loop becomes 2. As it is less than 7, so the condition becomes false. It will again moves towards the inner loop. The iteration in inner loop us from 0-7. So, it will display "01234567".It will again moves towards the outer loop.

This process continues until the value in the outer loop becomes 8. As 8 is less than 7, so the condition becomes false. As the condition in the outer loop becomes false, it will not jump towards the inner loop and the loop will terminate.