Qn: Write the C++ program logic to print stars as follows
* * * * * * * * *
* * * *   * * * *
* * *       * * *
* *           * *
*               *
* *           * *
* * *       * * *
* * * *   * * * *
* * * * * * * * *
-By Admin, Last Update On 12th June,2019 08:57 pm

இதை கீழ்க்கண்டவாறு இரண்டாக பிரித்து. அவற்றின் logic-களை ஒன்றாக இணைத்தால் நமக்கு தேவையான output கிடைத்துவிடும்.

part-1
* * * * * * * * *
* * * *   * * * *
* * *       * * *
* *           * *
*               *

part-2
*               *
* *           * *
* * *       * * *
* * * *   * * * *
* * * * * * * * *

இவற்றை ஒன்றாக இணைக்கும்போது part-1ல் உள்ள கடைசி row மற்றும் part-2ல் உள்ள முதல் row இவற்றில் எதாவது ஒன்றை மட்டும் தான் எடுத்துகொள்ளவேண்டும்.

//part-1 logic
for(p=1;p<=(2*n)-1;p++){
    Logic to print first row stars here..
}
cout<<endl;
for(int row=n-1;row>=1;row--){
    for(col=1;col<=row;col++){
    Logic to print stars here..
    }
    for(s=1;s<=(2*n-2*row)-1;s++){
    Logic to print space here..
    }
    for(col=1;col<=row;col++){
    Same Logic to print stars here..
    }
    cout<<endl;
}

//part-2 logic
for(row=2;row<=n-1;row++){
    for(col=1;col<=row;col++){
    Logic to print stars here..
    }
    for(s=1;s<=(2*n-2*row)-1;s++){
    Logic to print space here..
    }
    for(col=2;col<=row;col++){
    Same Logic to print stars here..
    }
    cout<<endl;
}
for(p=1;p<=(2*n)-1;p++){
    Logic to print last row stars here..
}

Complete Program

#include<iostream.h>
#include<conio.h>
int main(){
  int n,row,col,s,p;
  cout<<"Enter how many rows you want: ";
  cin>>n;
  //print first row stars
  for(p=1;p<=(2*n)-1;p++){
      cout<<"*";
  }
cout<<endl;
  //row should start with  n-1 end with 1. its important
  for(row=n-1;row>=1;row--){
      //print stars
      for(col=1;col<=row;col++){
          cout<<"*";
      }
      //print space
      for(s=1;s<=(2*n-2*row)-1;s++){
          cout<<" ";
      }
      //print stars
      for(col=1;col<=row;col++){
         cout<<"*";
      }
     cout<<endl;
  }
  //row should end with n-1 its important
  for(row=2;row<=n-1;row++){
      //print stars
      for(col=1;col<=row;col++){
          cout<<"*";
      }
      //print space
      for(s=1;s<=(2*n-2*row)-1;s++){
         cout<<" ";
      }
      //print stars
      for(col=1;col<=row;col++){
        cout<<"*";
      }
    cout<<endl;
  }
  //print last row stars
  for(p=1;p<=(2*n)-1;p++){
      cout<<"*";
  }  
return 0;
}
Output:
Enter how many rows you want: 5
* * * * * * * * *
* * * *   * * * *
* * *       * * *
* *           * *
*               *
* *           * *
* * *       * * *
* * * *   * * * *
* * * * * * * * *

Pgcomments

Comments