Nesting of for loops : Pyramid Drawing

Nesting of loops, that is, one for statement within another for statement, is allowed in C.

Theme: Inner Loop and Outer Loop Practice Using For in C
 
Write a C Program to print half pyramid using * as shown in figure below.
 
*
* *
* * *
* * * *
* * * * *


 
main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("* ");
        }
        printf("\n");
    }
    return 0;
}
 
 
 
 
Print half pyramid using numbers as shown in figure below.

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
 
main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("%d ",j);
        }
        printf("\n");
    }
    return 0;
} 
 

Write a C Program to print triangle of characters as below.
 
A
B B
C C C
D D D D
E E E E E
 
main()
{
    int i,j;
    char input,temp='A';
    printf("Enter uppercase character you want in triangle at last row: ");
    scanf("%c",&input);
    for(i=1;i<=(input-'A'+1);++i)
    {
        for(j=1;j<=i;++j)
           printf("%c",temp);
        ++temp;
        printf("\n");
    }
    return 0;
} 

Print inverted half pyramid using * as shown below. 

* * * * *
* * * *
* * * 
* *
*
 
main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=rows;i>=1;--i)
    {
        for(j=1;j<=i;++j)
        {
           printf("* ");
        }
    printf("\n");
    }
    return 0;
} 

Print inverted half pyramid as using numbers as shown below.
 
1 2 3 4 5
1 2 3 4 
1 2 3
1 2
1 
 
main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=rows;i>=1;--i)
    {
        for(j=1;j<=i;++j)
        {
           printf("%d ",j);
        }
    printf("\n");
    }
    return 0;
} 

C program to print pyramid using *
 
     
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
 
 
#include <stdio.h>
int main()
{
    int i,space,rows,k=0;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;++i)
    {
        for(space=1;space<=rows-i;++space)
        {
           printf("  ");
        }
        while(k!=2*i-1)
        {
           printf("* ");
           ++k;
        }
        k=0;
        printf("\n");
    }
    return 0;
} 
 

C Program to display Floyd's Triangle

1
2 3
4 5 6
7 8 9 10
 
#include<stdio.h>
int main()
{
    int rows,i,j,k=0;
    printf("Enter number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;i++)
    {
        for(j=1;j<=i;++j)
          printf("%d ",k+j);
        ++k;
        printf("\n");
    }
} 
 
 

3 comments:

  1. #include
    main()
    {
    int a,b;
    for(a=1;a<=5;a++)
    {
    for(b=1;b<=a;b++)
    printf("%d",b);
    printf("\n");
    }
    }

    ReplyDelete
  2. id: 201510646

    #include
    using namespace std;
    int main()
    {
    int num,i, j;
    cout<<"How many lines do you want? ";
    cin>>num;
    for(i=1; i<=num; i++)
    {
    for(j=1; j<+i; j++)
    cout<<"* ";
    cout<<endl;
    }
    cout<<endl;
    }

    ReplyDelete
  3. id: 201510646

    #include
    using namespace std;
    int main()
    {
    int num,i, j;
    cout<<"How many lines do you want? ";
    cin>>num;
    for(i=1; i<=num; i++)
    {
    for(j=1; j<=i; j++)
    cout<<" "<<j ;
    cout<<endl;
    }
    cout<<endl;
    }

    ReplyDelete