Pattern Question (stars pattern in ascending order) with source code and small explanation 04 || 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 for loop again till b for column
- Spaces are available here till total_column - row_number i.e. b-i after that we will print * till b
- here we are checking condition is that j (refers to column) is greater that b-i(total_column - row_number , total spaces which print first ) if condition gets true than print starts else print spaces.
- 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; j++)
{
if(j > b-i){
cout<<"*";
}else{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}
Comments
Post a Comment