Write a program to print Pyramid pattern using stars

07 January 2023

Write a program to print Pyramid pattern using stars



Description

 

Get the number of lines as the input and print stars in that many lines or rows in a pyramid shape.

Input

4

Output

      *

    ***

  *****

*******

C Program

#include <stdio.h>

int main() 

{

    int i, j, k, rows;

    printf("Enter number of rows");

    scanf("%d", &rows);

    for(i=1; i<=rows; i++)

    { 

        for(j=i; j<rows; j++)

                    printf(" ");

       for(k=1; k<=(2*i-1); k++)

                    printf("*");

       printf("\n");

    }

    return 0;

}

C++ Program

#include <iostream>

using namespace std;

int main() 

{

    int i, j, k, rows;

    cout<<"Enter number of rows: ";

    cin>>rows;

    for(i=1; i<=rows; i++)

    { 

        for(j=i; j<rows; j++)

            cout<<" ";

       for(k=1; k<=(2*i-1); k++)

            cout<<"*";

       cout<<"\n";

    }

    return 0;

}

Java

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of rows: ");

int rows = sc.nextInt();

for(int i=1; i<=rows; i++)

    { 

        for(int j=i; j<rows; j++)

            System.out.print(" ");

       for(int k=1; k<=(2*i-1); k++)

            System.out.print("*");

       System.out.println("");

    }

}

}

Python

rows=int(input("Enter the number of rows: "))

for i in range(0,rows+1):

    for j in range(i,rows):

        print(" ",end="")

    for k in range(0,2*(i-1)+1):

        print("*",end="")

    print()


 If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Related Articles

Ask Us Anything !