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 :

  1. header file include
  2. use namespace which standard in c plus plus
  3. Program's starting point : main() function
  4. declared two variable a, b
  5. get input of variable a,b
  6. use for loop for row till a
  7. use for loop again till b for column
  8. Spaces are available here till total_column - row_number i.e. b-i after that we will print * till b
  9. 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.
  10. break line using line break endl after completing row.
  11. 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