Q. Write a program to print half pyramid pattern using stars.



Half Pyramid Example:-

Inverted Half Pyramid Pattern



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


Inverted Half Pyramid Program

  • C
  • C++
  • Java
  • Python
  • #include<stdio.h>
    int main() {
        int i, j, rows;
        printf("Enter number of rows: ");
        scanf("%d", &rows);
        for (i=rows; i>=1; --i) {
            for (j=1; j<=i; ++j)
            { printf("* "); }
            printf("\n");
        }
        return 0;
    }
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int rows;
    
        cout << "Enter number of rows: ";
        cin >> rows;
    
         for (int i=rows; i>=1; --i)
        {
            for(int j = 1; j <= i; ++j)
            {
                cout << "* ";
            }
            cout << "\n";
        }
        return 0;
    }
    import java.util.*;
    public class LFC {
    
        public static void main(String[] args) {
             int rows;
             System.out.println("Enter number of rows: ");
            Scanner sc = new Scanner(System.in);
            rows = sc.nextInt();
    
             for (int i=rows; i>=1; --i){
                for(int j = 1; j <= i; ++j) {
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
    
    def halfpy(n): 
        
        for i in range(n, 0, -1): 
          
            for j in range(0, i): 
              
                # printing stars 
                print("* ",end="") 
           
            # ending line after each row 
            print("\r") 
      
    # Driver Code
    print("Enter number of rows: ") 
    n = int(input()) 
    halfpy(n) 
    
    #include<stdio.h>
    int main() {
        int i, j, rows;
        printf("Enter number of rows: ");
        scanf("%d", &rows);
        for (i=rows; i>=1; --i) {
            for (j=1; j<=i; ++j)
            { printf("* "); }
            printf("\n");
        }
        return 0;
    }
    

    Output

    Enter number of rows: 5
    
    
    *  *  *  *  * 
    *  *  *  *
    *  *  *
    *  *
    *
    

    Recommended Programs

       Program to print half pyramid pattern.
       Program to print floyds triangle.