About  Contact


Programs Using Nested Loops

Write a programs to display the following patterns-
Q.36
        *
        **
        ***
        ****
        *****
Q.37
       *****
       ****
       ***
       **
       *
Q.38
         *
        **
       ***
      ****
     *****
Q.39
         *****
          ****
           ***
            **
             *
Q.40
         12345
         1234
         123
         12
         1
Q.41
         12345
          1234
           123
            12
             1
Q.42
         11111
          2222
           333
            44
             5
Q.43
         5
         44
         333
         2222
         11111
Q.44
         a
         bc
         def
         ghij
         klmno
Q.45
         0
         12
         345
         6789
Program-36
#include <iostream.h>
void main(){
  int i, j;
  for(i=1; i<= 5; i++){
    for(j=1; j<=i; j++){
      cout<<"*";
    }
	 cout<<"\n";
  }
}

In this program looping variable "i" is counting number of lines, that is why it is iterating from 1 to 5; in the inner loop the variable "j" is counting the number of stars in each line, so it iterates from 1 to the current value of "i" i.e 1, 2, 3, 4 & 5. The last "cout" statement is within outer loop, which breaks line.
Program-37
#include <iostream.h>
void main(){
  int i, j;
  for(i=5; i>= 1; i--){
    for(j=1; j<=i; j++){
      cout<<"*";
    }
	 cout<<"\n";
  }
}
In this program the outer loop iterates from 5 to 1, other things as earlier.
Program-38
#include <iostream.h>
void main(){
  int i, j, k=20, s;
  for(i=1; i<= 5; i++){
    for(s=1; s<=k; s++){
      cout<<" ";
    }
    for(j=1; j<=i; j++){
      cout<<"*";
    }
    k=k-1;
	 cout<<"\n";
  }
}
Before I explain this program let me say one thing that when computer displays anything it is always left aligned. That is why we did not do anything while displaying the previous patters. But see in this program the pattern is right aligned, so we have to take measure to align the pattern right ourself. Now see the program the loop s=1 to s=k is displaying 20 space (give one space within the quotes with the "cout") in the first iteration of outer loop ten it displays one star, then see value of k is decreasing by 1 in every iteration of outer loop. so in second iteration of outer loop there will be 19 spaces then two stars and so on.
Program-39
#include <iostream.h>
void main(){
  int i, j, k=20, s;
  for(i=5; i>= 1; i--){
    for(s=1; s<=k; s++){
      cout<<" ";
    }
    for(j=1; j<=i; j++){
      cout<<"*";
    }
    k=k-1;
	 cout<<"\n";
  }
}
This program is same as the previous one, only take the outer loop in reverse order.
Program-40
#include <iostream.h>
void main(){
  int i, j;
  for(i=1; i<= 5; i++){
    for(j=1; j<=i; j++){
      cout<<j;
    }
	 cout<<"\n";
  }
}
In this program we are displaying the value of j. In iteration - 1 of the outer loop it displays 1 to 5, second iteration 1 to 4 and so on.

Program-41
#include <iostream.h>
void main(){
  int i, j, k=20, s;
  for(i=5; i>= 1; i--){
    for(s=1; s<=k; s++){
      cout<<" ";
    }
    for(j=1; j<=i; j++){
      cout<<j;
    }
    k=k-1;
	 cout<<"\n";
  }
}
In this program dispaly the value of "j" and take measure for right alignment.
Program-42
#include <iostream.h>
void main(){
  int i, j, k=20, s;
  for(i=5; i>= 1; i--){
    for(s=1; s<=k; s++){
      cout<<" ";
    }
    for(j=1; j<=i; j++){
      cout<<i;
    }
    k=k-1;
	 cout<<"\n";
  }
}
In this program dispaly the value of "i" as for all the iterations of the loop with looping variable "j" the value of "i" remain same, take measure for right alignment.
Program-43
#include <iostream.h>
void main(){
  int i, j;
  for(i=5; i>= 1; i--){
    for(j=5; j>=i; j--){
      cout<<i;
    }
	 cout<<"\n";
  }
}
In this program take both the loops in reversed oder and display the value of "i".

Program-44
#include <iostream.h>
void main(){
  int i, j, ch=97;
  for(i=1; i<= 5; i++){
    for(j=1; j<=i; j++){
      cout<<char(ch);
      ch=ch+1;
    }
	 cout<<"\n";
  }
}
The characters that a computer displays on screen has got a numeric code which is called ASCII code (American Standard Code for Information Interchange). 97 is the code of "a", 98 is code of "b" and so on. Similarly 65 is code of "A", 66 is code of "B" and so on. No see in the program a variable "ch" is initialized to 97, in the inner loop char(ch) will display the character corresponding to the value given by "ch", so first time it is "a", then "b" and so on as "ch" is increased by 1 after displaying the character. The rest is already discussed.

Program-45
#include <iostream.h>
void main(){
  int i, j, p=0;
  for(i=1; i<= 5; i++){
    for(j=1; j<=i; j++){
      cout<<p;
      p=p+1;
    }
	 cout<<"\n";
  }
}
This program is same as the previous one, here it displaying continuous numeric value given by the variable "p".




Disclaimer: We have taken otmost care to keep the information error free. The site is not responsible for any kind of damage of whatsoever nature.
©Copy right reserved