C++ Program to display pyramid of digits

Thursday, 22 December 2016

Statement:
C++ Program to display pyramid of digits
Code : 
//C++ Program to display pyramid of digits. and rows entered by user
#include<iostream>

using namespace std;
int main()
{
       cout << "\t\t\tPyramid Of digits\n\n";
       int num,spc,i, j, k, l;
       k = 0; l = 0;
              cout<<"Enter the number of rows: ";
              cin >> num;
              for (i = 1; i <= num; i++) //loop for control user entered number and newline
              {
                     for (spc = 1; spc <= num - i; spc++) //spc is a variable for spaces and this loop is to control spaces
                     {
                           cout << " ";
                           k++;
                     }
                     for (j=0; j != 2 * i - 1;j++) //this loop will print numbers by condtion
                     {
                           if (k <= num - 1) // this condtion will print numbers brfore maximum interval
                           {
                                  cout<<i + j;
                                  k++;
                           }
                           else //print numbers after maximum number
                           {
                                  l++;
                                  cout<<(i + j - 2 * l);
                           }
                          
                     }
                     l = 0; k = 0; j = 0; //updating vluse of i j and k before next printing
                     cout<<"\n"; //add newline after once line print
              }
       cout << "\n\n";
       system("pause");
       return 0;

}

Mohammad Tayyab

Author & Editor

Imagination is more important than knowledge.

0 comments:

Post a Comment