46. Program to generate the following pyramid of digits using nested loop.

Chapter 6: 

Looping Structures

Programming Exercise

Problem # 46:

Write a program to generate the following pyramid of digits using nested loop:
                      1
                    232
                  34543
                4567654
              567898765
            67890109876
          7890123210987
        890123454321098
      90123456765432109
    0123456789876543210

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

    int i, j, m, n, k, l, o;
    for(i=1,n=0;i<=10;i++,n+=2)
    {
        if(n==10)
        n=0;
        for(k=1;k<=10-i;k++)
        {
            cout<<" ";  
        }
        for(j=i,l=1;l<=i;j++,l++)
        {
            if(j==10)
            j=0;
          
            cout<<j;
        }
        for(m=n,o=1;o<i;m--,o++)
        {
            if(m==-1)
            m=9;
            cout<<m;
        }
        cout<<"\n";        
    }  
}

Output:

Chapter 6, Coding tutorial, Computer Science, CPP Basics, Exercise Solution, IT Series, Looping Structures, Object Oriented Programming using C++, Programming for beginners, Programming Tutorial

Let me know in the comment section if you have any question.

Previous Post:
45. Program that displays a diamond of asterisks using loop.

Comments