Program to print inverted pyramid star pattern

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

Problem Statement:- Program to print an inverted pyramid star pattern.

Sample Input/Output:-


Sample Input First:5

Sample Output First: 

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

Sample Input Second: 4


Sample Output Second: 

* * * *
 * * *
  * *
   *


Data requirement:-


  Input Data:- row_size


  Output Data:- *


  Additional Data:- in,out,p


Program in C

Here is the source code of the C Program to print an inverted pyramid star pattern.


Code:


#include<stdio.h>
int main()
{
  printf("Enter the row size:");
  int row_size,in,out,p;
  scanf("%d",&row_size);
  for( out=1;out<=row_size;out++)
  {
   for( in=1;in<out;in++)
    printf(" ");

   for(p=row_size;p>=out;p--)

    printf("* ");

   printf("\n");

}}

Input/Output:
Enter the row size:5
* * * * *
 * * * *
  * * *
   * *
    *

Program in C++

Here is the source code of the C++ Program to print an inverted pyramid star pattern.


Code:


#include<iostream>
using namespace std;
int main()
{
  cout<<"Enter the row size:";
  int row_size,in,out,p;
  cin>>row_size;
  for( out=1;out<=row_size;out++)
  {
   for( in=1;in<out;in++)
    cout<<" ";

   for(p=row_size;p>=out;p--)

    cout<<"* ";

   cout<<"\n";

}}


Input/Output:
Enter the row size:4
* * * *
 * * *
  * *
   *

Program in Java

Here is the source code of the Java Program to print an inverted pyramid star pattern.


Code:


import java.util.Scanner;
public class p6 {

public static void main(String[] args) {

Scanner cs=new Scanner(System.in);
System.out.println("Enter the row size:");

  int row_size=cs.nextInt();


  for(int out=1;out<=row_size;out++)

  {
   for(int in=1;in<out;in++)

    System.out.printf(" ");

   for(int in=row_size;in>=out;in--)
    System.out.printf("* ");

   System.out.println();

  }
cs.close();
}
}

Input/Output:
Enter the row size:
6
* * * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 

Program in Python

Here is the source code of the Python Program to print an inverted pyramid star pattern.


Code:


print("Enter the row size:")
row_size=int(input())
for out in range(row_size+1):
    for j in range(out):
        print(" ",end="")
    for p in range(row_size,out,-1):
        print("* ",end="")
    print("\r")

Input/Output:
Enter the row size:
6
* * * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 


Post a Comment

0 Comments