Pattern Question (stars pattern in descending order) with source code and small explanation 03 || Print pattern using c++ with 5 rows and 5 columns
Here I am going to print following pattern using c plus plus language in pattern question
ALGORITHM FOR PRINTING STAR PATTERN IN DESCENDING ORDER :
- header file include
- use namespace which standard in c plus plus
- Program's starting point : main() function
- declared two variable a, b
- get input of variable a,b
- use for loop for row till a
- use again for loop for column till b-(i-1) , here suppose b = 5 and i = 1 Now for first row where i=1 is running till 5-(1-1)=5 five. like this our loop work if i = 2 than loop will go through 5-(2-1)=4 four and so on.
- print * stars using cout
- break line using line break endl after completing row.
- return 0 to show that there is no error in the program.
YOU CAN COPY THIS CODE AS WELL !
#include <iostream>
using namespace std;
int main(){
int a , b;
cin>>a>>b;
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= b-(i-1); j++)
{
cout<<"* ";
}
cout<<endl;
}
return 0;
}
Comments
Post a Comment