Sunday, 23 July 2017

Pattern Programs Using Nested loops

Repetition (nested loop) 

Pattern Programs Using Nested loops:

These program prints many different patterns of stars and etc. These codes illustrate how to create various patterns using nested loop in C programming. Most of these programs involve usage of nested loops . A pattern of numbers, star or characters is a way of arranging these in some logical manner or they may make a sequence. Some of these patterns are triangles which have special importance in mathematics. Some patterns are symmetrical while other are not. See the complete page and know how the nested loops work and behave in C language thanks.
Write the programs which displays the following :
1. a Diamond of Dollars
2. a Slide of #
3. a left Slide of @
4. a negative Slide of $
5. a Triangle of  *
6. a Side Shape Triangle of *


Right here are some extraordinarily easy applications in C++ for beginner students. I am hoping those might help them recognize how nested loops works in programming. The required pattern of numbers to be printed on display screen by means of a particular software is given earlier than the supply code to serve a problem statement.                                                      

What is a Nested Loop ?


A loop inner any other loop is referred to as a nested loop. ... We will have any wide number of nested loops as required. Remember a nested loop where the outer loop runs n times and includes any other loop inner it. The internal loop runs m times. Then, the total number of times the internal loop runs all through the program execution is n*m.

How Nested Loop work in C ?

Here a simple program that shows what is logic and how the nested loop works:
Example:
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
Statements
}
}
Right here to start with the cost if i is 0 and j loop (inner loop) receives finished for 10 times as j is from 0-9 then the inner loop fails its circumstance and control is going lower back to outer loop which is incremented from 0 to 1 on the other hand the internal loop receives executed from 0-9 the equal aspect repeats until outer loop is carried out from 0-9 after outer loop condition fail the rest of this system is finished.

Now the Solution of above questions:

1. a Diamond of Dollars:

                                                 $                                          
                                             $  $  $                
                                         $  $  $  $  $                                
                                     $  $  $  $  $  $  $                            
                                         $  $  $  $  $                                  
                                             $  $  $                                 
                                                $

To print above pattern see the code below:

Code:


#include<stdio.h>

#include<conio.h>
void main()
{
 int r,c,sp;
 for(r=1;r<=4;r++)
 {
  for(sp=4-r;sp>=1;sp--)
  printf(" ");
  for(c=1;c<=r;c++)
  printf("$");
  for(c=r-1;c>=1;c--)
  printf("$");
 printf("\n");
 }
 for(r=1;r<=4;r++)
 {
  for(sp=r;sp>=1;sp--)
  printf(" ");
  for(c=1;c<=(4-r);c++)
  printf("$");
  for(c=4-r-1;c>=1;c--)
  printf("$");
 printf("\n");
 }
 getch();
 clrscr();
}

Output:


Try it yourself.
If you have any problem you can tell me on comment box i will help you.

2. a Slide of #

                                                                                              #
                                                                                               #  #
                                                                                               #  #  #
                                                                                               #  #  #  #
                                                                                               #  #  #  #  #
                                                                                               #  #  #  #  #  #
To print above pattern see the code below:

Code:


#include<stdio.h>

#include<conio.h>
void main()
{
 int a,b;
 for(a=1;a<=6;a++)
 {
 for(b=1;b<=a;b++)
 {
 printf(" # ");
 }
 printf("\n\n");
 }
 getch();
 clrscr();
}

Output:

Try it yourself.
If you have any problem you can tell me on comment box i will help you.
3. a left Slide of @                                                

                                               @                          

                                           @@       
                                        @@@        
                                    @@@@       
                                 @@@@@      

To print above pattern see the code below:

Code:

#include<stdio.h>

#include<conio.h>
void main()
{
 int a,b,c;
 for(a=1;a<=5;a++)
 {
 for(b=4;b>=a;b--)
 {
 printf(" ");
 }
 for(c=1;c<=a;c++)
 {
 printf("@");
 }
 printf("\n");
 }
 getch();
 clrscr();
}

Output:


Try it yourself.
If you have any problem you can tell me on comment box i will help you.

4.a negative Slide of $

                                                                        $$$$$$$$$$              
                                                                        $$$$$$$$
                                                                        $$$$$$
                                                                        $$$$
                                                                        $$ 

To print above pattern see the code below:

Code:


#include<stdio.h>

#include<conio.h>
void main()
{
 int a,b;
 for(a=5;a>=1;a--)
 {
 for(b=1;b<=a;b++)
 {
 printf("$$",b);
 }
 printf("\n");
 }
 getch();
 clrscr();
}

Output:


Try it yourself.
If you have any problem you can tell me on comment box i will help you.

5 .a Triangle of  *

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

To print above pattern see the code below:

Code:

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b,c;
 for(a=1;a<=8;a+=2)
 {
 for(b=2;b<=a;b++)
 {
 printf(" ");
 }
 for(c=7;c>=a;c--)
 {
 printf(" *");
 }
 printf("\n");
 }
 getch();
 clrscr();
}

Output:



6. a Side Shape Triangle of *

*  *  *
*  *  *  * 
*  *  *  *  * 
*  *  *  *  *  *
*  *  *  *  * 
*  *  *  * 
*  *  *
To print above pattern see the code below:
Code: 

#include<stdio.h>

#include<conio.h>
void main()
{
int n,r,c;
 for(r=3;r<=6;r++)
 {
  for(c=1;c<=r;c++)
 {
  printf("*");
 }
  printf("\n");
 }
 for(n=5;n>=3;n--)
 {
 for(c=1;c<=n;c++)
 {
 printf("*");
 }
 printf("\n");
 }
 getch();
 clrscr();
}

Output:


Try it yourself.
If you have any problem you can tell me on comment box i will help you.



Using these examples you are in a better position to create your desired pattern for yourself. Creating a pattern involves how to use nested loops properly, some pattern may involve alphabets or other special characters. Key aspect is knowing how the characters in pattern changes.


Viral Programs

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment