C++ OOP program to print pyramid of a number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include<iostream> // header file using namespace std; //name space class projectsinventory_DotCom // class is created with name of "projectsinventory_DotCom" { protected: int i,j,k,l,n; //"i,j,k,l,n" are variable names which are created by int data type // it is protected which accessiable by only this and childs class(If any) public: int show() // show is function and it is public.That means this function is accesiable anywhere outside the class { // starting the show function cout<<"num of rows :"; // user have to enter number of rows cin>>n; // input in "n" name variable for(i=1;i<=n;i++) // loop is started with no 1 ,and in which i is lessthan equal to n,an increment in loop // until the condition is true { for(l=1;l<=n-i;l++) // for loop having another loop which started with num 1,1 is lessthan equal to n-i // loop having increment if the condition is true { cout<<" "; // adding spacing } for(j=1;j<=i;j++) // loop is starting in which j variable equal to 1,in loop j is lessthan equal to i // loop having increment if condition is true { cout<<j; // display the value of which is stored in the variable j } for(k=i-1;k>=1;k--) // loop is started in which variable k stored i-1,k is greaterthan equal to 1 // if condtion is true loop having decrement { cout<<k; // display the value which is stored in variable k } cout<<endl; // going to next line } } }; int main() // main function is performing { projectsinventory_DotCom obj; //projectsinventory_DotCom is the class and obj is the object of the class projectsinventory_DotCom. //we can access the show funtion of class projectsinventory_DotCom with the help of this object. obj.show(); //show function is a property of object obj and obj is a object of class projectsinventory_DotCom } |