Skip to main content

20+ Most Asked Star Pattern Coding Questions and Solutions in C++ / Python / Java / C Language

20+ Most Asked Star Pattern Coding Questions and Solutions in C++ / Python / Java / C Language  



Star patterns are one of the most commonly asked questions in programming interviews and coding contests. They are a great way to practice logic and pattern recognition skills. In this article, we will discuss different types of star patterns, their logic, and how to code them.

To form patterns, we need to use nested loops. The outer loop will be responsible for the number of rows, and the inner loop will print the pattern like star, number or alphabet.

1. Right Angle Triangle Star Pattern 

* * 

* * * 

* * * * 

#include <iostream>

using namespace std;

int main()

{

    int n=4;    

    for(int i=0;i<n;i++){

        for(int j=0;j<=i;j++){

            cout<<"* ";

        }

        cout<<endl;

    }

    return 0;

}


2. Inverted Right Angle Triangle Star Pattern 

*****

****

***

**

*


#include <iostream>

using namespace std;

int main()

{

    int n=5;

    for(int i=n;i>=1;i--){

        for(int j=1;j<=i;j++){

            cout<<"*";

        }

        cout<<endl;

        }

    return 0;

}


3. Right Angle Triangle Star Pattern (Another Approach)

*

**

***

****

*****


#include <iostream>

using namespace std;

int main()

{

    int n = 5;

    for(int i=1;i<=n;i++){

        for(int space=1;space<=n-i;space++){

            cout<<" ";

        }

        for(int j=1;j<=i;j++){

            cout<<"*";

        }

    cout<<endl;

    }

    return 0;

}


4. Right Angle Triangle with spaces Star Pattern


    *

   **

  ***

 ****

*****

#include <iostream>

using namespace std;

int main()

{

    int n = 5;

    for(int i=1;i<=n;i++){

        for(int space=1;space<=n-i;space++){

            cout<<" ";

        }

        for(int j=1;j<=i;j++){

            cout<<"*";

        }

    cout<<endl;

    }

    return 0;

}


5. Inverted Right Angle Triangle with spaces Star Pattern 


*****

 ****

  ***

   **

    *


#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    
    for(int i=n;i>=1;i--){
        
        for(int space=n-i;space>=1;space--){
            cout<<" ";
        }
        
        for(int j=1;j<=i;j++){
            cout<<"*";
        }

    cout<<endl;
    }

    return 0;
}

6. Square Star Pattern 

* * * * * 
* * * * * 
* * * * * 
* * * * * 
* * * * * 

#include <iostream>

using namespace std;

int main()
{
    int n=5;
    
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cout<<"* ";
        }
        cout<<endl;
    }

    return 0;
}


7. Pyramid Star Pattern 

    *
   ***
  *****
 *******
*********

#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    
    for(int i=1;i<=n;i++){
        for(int space=1;space<=n-i;space++){
            cout<<" ";
        }
        for(int j=1;j<=2*i-1;j++){
            cout<<"*";
        }
    cout<<endl;
    }

    return 0;
}


8. Inverted Pyramid Star Pattern 

*********
 *******
  *****
   ***
    *
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    
    for(int i=n;i>=1;i--){
        for(int space=n-i;space>=1;space--){
            cout<<" ";
        }
        for(int j=1;j<=2*i-1;j++){
            cout<<"*";
        }
    cout<<endl;
    }

    return 0;
}


    * 
   * * 
  * * * 
 * * * * 
* * * * * 

9. Pyramid Star Pattern (Another Approach)

    * 
   * * 
  * * * 
 * * * * 
* * * * * 

#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    
    for(int i=1;i<=n;i++){
        for(int space=1;space<=n-i;space++){
            cout<<" ";
        }
        for(int j=1;j<=i;j++){
            cout<<"* ";
        }
    cout<<endl;
    }

    return 0;
}


10. Inverted Pyramid Star Pattern (Another Approach)

* * * * * 
 * * * * 
  * * * 
   * * 
    * 

#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    
    for(int i=n;i>=1;i--){
        for(int space=n-i;space>=1;space--){
            cout<<" ";
        }
        for(int j=1;j<=i;j++){
            cout<<"* ";
        }
    cout<<endl;
    }

    return 0;
}


11. Sand Glass Star Pattern

* * * * * 
 * * * * 
  * * * 
   * * 
    * 
    * 
   * * 
  * * * 
 * * * * 
* * * * *

#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    
    for(int i=n;i>=1;i--){
        for(int space=n-i;space>=1;space--){
            cout<<" ";
        }
        for(int j=1;j<=i;j++){
            cout<<"* ";
        }
    cout<<endl;
    }
    
    for(int i=1;i<=n;i++){
        for(int space=1;space<=n-i;space++){
            cout<<" ";
        }
        for(int j=1;j<=i;j++){
            cout<<"* ";
        }
    cout<<endl;
    }
    
    

    return 0;
}


12. Diamond Star Pattern

    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 

#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    
    for(int i=1;i<=n;i++){
        for(int space=n-i;space>=1;space--){
            cout<<" ";
        }
        for(int j=1;j<=i;j++){
            cout<<"* ";
        }
    cout<<endl;
    }
    
    for(int i=n-1;i>=1;i--){
        for(int space=1;space<=n-i;space++){
            cout<<" ";
        }
        for(int j=1;j<=i;j++){
            cout<<"* ";
        }
    cout<<endl;
    }

    return 0;
}


13. Right Angle Hollow Triangle Star Pattern

*
**
* *
*  *
*****

#include <iostream>
using namespace std;

int main()
{
    int n=5;
    
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            if(j==1 || i==j || i==n){
                cout<<"*";
            }
            else{
                cout<<" ";
            }
        }
        cout<<endl;
    }

    return 0;
}


14. Inverted Right Angle Hollow Triangle Star Pattern

*****
 *  *
  * *
   **
    *

#include <iostream>
using namespace std;

int main()
{
    int n=5;
    
    for(int i=n;i>=1;i--){
        for(int j=n;j>=1;j--){
            if(j==1 || i==j || i==n){
                cout<<"*";
            }
            else{
                cout<<" ";
            }
        }
        cout<<endl;
    }

    return 0;
}


15. Hollow Pyramid Star Pattern

    *
   * *
  *   *
 *     *
*********

#include <iostream>
using namespace std;

int main()
{
    int n=5;
    
    for(int i=1;i<=n;i++){
        for(int space=1;space<=n-i;space++){
            cout<<" ";
        }
        
        for(int j=1;j<=2*i-1;j++){
            if(i==n || j==1 || j==2*i-1){
                cout<<"*";
            }
            else{
                cout<<" ";
            }
            
        }
        
        cout<<endl;
    }

    return 0;
}

16. Right Angle Triangle Star Pattern (Another Approach)

*****
****
***
**
*

#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n-i+1;j++){
            cout<<"*";
        }
    cout<<endl;
    }

    return 0;
}


17. Diamond Star Pattern (Another Approach)

    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 

#include <iostream>
using namespace std;

int main()
{
    int n=5;
    
    for(int i=1;i<=2*n-1;i++){
        int c = i>n ? 2*n-i: i;
        for(int space=1;space<=n-c;space++){
           cout<<" "; 
        }

        for(int j=1;j<=c;j++){
            cout<<"* ";
        }
        cout<<endl;
    }

    return 0;
}

18. Inverted Hollow Pyramid Star Pattern

*********
 *     *
  *   *
   * *
    *

#include <iostream>
using namespace std;

int main()
{
    int n=5;
    for(int i=n;i>=1;i--){
        for(int space=1;space<=n-i;space++){
            cout<<" ";
        }
        for(int j=1;j<=2*i-1;j++){
            if(j==1 || j==2*i-1 || i==n){
                cout<<"*";
            }
            else{
                cout<<" ";
            }
        }
        cout<<endl;
    }

    return 0;
}


19. Hollow Diamond Star Pattern

    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *

#include <iostream>
using namespace std;

int main()
{
    int n=5;
    for(int i=1;i<=n;i++){

        for(int space=1;space<=n-i;space++){
            cout<<" ";
        }
        for(int j=1;j<=2*i-1;j++){
            if(j == 1 || j==2*i-1){
                cout<<"*";    
            }
            else{
                cout<<" ";
            }
        }
        cout<<endl;
    }
    
    for(int i=n-1;i>=1;i--){

        for(int space=1;space<=n-i;space++){
            cout<<" ";
        }
        for(int j=1;j<=2*i-1;j++){
            if(j == 1 || j==2*i-1){
                cout<<"*";    
            }
            else{
                cout<<" ";
            }
        }
        cout<<endl;
    }

    return 0;
}

20. Right Angle Triangle Star Pattern using While Loop

*
**
***
****

#include <iostream>

using namespace std;

int main()
{
    int n=4;
    int i=1,j=1;
    while(i<=n){
        while(j<=i){
            cout<<"*";
            j++;
        }
        j=1;
        cout<<endl;
        i++;
    }

    return 0;
}

Bonus- Right Angle Triangle Number Pattern 

1
12
123
1234
12345

#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            cout<<j;
        }
    cout<<endl;
    }

    return 0;
}

Comments

Popular posts from this blog

TechGig Code Gladiators 2023 Coding Solutions

TechGig Open Coding Round Code Gladiators 2023 Answers Round 1  - Problem 1 Forest Fire Coding Solution Figure out the minimum energy level P such that they can get exactly X animals to transport.  Test Case: 5 4  1 3 2 4 5 Output: 2 Coding Solution in Python 3: from bisect import bisect_left #Telegram - @PLACEMENTLELO n, m = map ( int , input ().split()) a = list ( map ( int , input ().split())) a.sort() #Telegram - @PLACEMENTLELO if n - bisect_left(a, a[n-m]) != m: print ( -1 ) else : print (a[n - m]) #Telegram - @PLACEMENTLELO ------------------------------------------- Round 2  - Problem 2 The Magic Coding Solution The cost of using wand for operation on element equal to absolute difference between value of element and desired value after operation. Test Case: 5 3 1 2 3 4 5 5 2 1 Output: 10 7 10 Coding Solution in Python 3: from bisect import bisect_left #Telegram - @PLACEMENTLELO n, m = map ( int , input ().split()) A = list ( map ( int , input ().split())) Q = list

The Divisibility Dilemma Code Techgig Geek Goddess Solution

The Divisibility Dilemma Code Techgig Geek Goddess Solution 100% Working Code uploaded on Youtube: https://youtu.be/6cn7nNtSiKo 100% Working Code uploaded on Telegram: https://telegram.me/PLACEMENTLELO The Divisibility Dilemma (100 Marks) In the picturesque village of Numeropolis, the annual "Divisibility Day" is celebrated with great enthusiasm. This day is marked by various mathematical challenges and games that put the villagers' arithmetic skills to the test. This year, the villagers are embarking on a quest to explore the distinctive properties of subarrays within integer arrays. The village's renowned mathematician, Professor Arithmo, has crafted an intriguing challenge. He has provided the villagers with an integer array nums`and two additional integers, K and P. Professor Arithmo's challenge revolves around discovering and counting distinct subarrays from the array nums. The uniqueness of these subarrays is determined by their divisibility characteristics.

Groundtruth Exam Answers

Groundtruth Exam Solutions  1. A company produces electronic devices with a defect rate of 5%. They implement a new quality control system that reduces the defect rate to 2%. By what percentage did the defect rate decrease 2. A construction project is scheduled to be completed in 40 days with a team of 10 workers. Due to unforeseen circumstances, three workers are unavailable for the project. How many additional days will it take to complete the project with the remaining workers? 3. A rectangular field has a length twice its width. The perimeter of the field is 60 meters. What is the area of the field?  4. A car travels a distance of 240 kilometers with an average speed of 60 km/h. For how many hours did the car travel?  5. A water tank can be filled by two pipes. Pipe A can fill the tank in 6 hours, while Pipe B can fill the tank in 4 hours. How long will it take to fill the tank if both pipes are open simultaneously? 1. Which of the following is an example of a sustainable engineeri