Pattern Question (print number pattern in c plus plus) with source code and small explanation 06 || Print pattern using c++ with 5 rows and 5 columns
Here I am going to print following pattern ( the number pattern ) using c plus plus language.
ALGORITHM FOR PRINTING STAR PATTERN IN ASCENDING ORDER OF ROWS :
- header file include
- use namespace which standard in c plus plus
- Program's starting point : main() function
- declared two variable a
- also initialize on variable count with value 0
- get input of variable a
- use for loop for row till a
- use again for loop till current row number (i) for getting column.
- to achieve this pattern we have to print counter numbers in increasing order (like 1,2,3,4,5...) only till column(j) which is equal to row number (i)
- here we are using increament operator ++ for increasing value by one 1.
- print row number using cout
- break line using line break endl after completing row.
- return 0 to show that there is no error in the program.
#include <iostream>
using namespace std;
int main(){
int a ,count=1;
cin>>a;
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= i; j++)
{
cout<< count++ <<" ";
}
cout<<endl;
}
return 0;
}
Comments
Post a Comment