Print Hollow Pyramid Star Pattern
PRINTING PATTERN:
*
* *
* *
*******
PREREQUISITE:
Basic knowledge of C language and use of loops.
ALGORITHM:
- Take the number of rows as input from the user and store it in any variable.(‘r‘ in this case).
- Run a loop ‘r’ number of times to iterate through each of the rows. From i=0 to i<r. The loop should be structured as for( i=0 ; i<r : i++).
- Run a nested loop inside the main loop to print the spaces before the pyramid. From k=r to k>i +1. The loop should be structured as for( k=r; k>i+1 ;k–).
- Inside this nested loop print white space.
- Run another nested loop inside the main loop after the previous loop to print the stars in each column of a row. From j=0 to j<=i*2. The loop should be structured as for( j=0 ; j<i*2; j++).
- Inside this nested loop print star under the condition if(i==r-1).
- Under else use a nested if else statement to print the remaining stars if(j==0||j>=i*2) and else print white space
- Inside main loop move to the next line by printing a new line . printf(“/n”).
Code in C:
#include<stdio.h> int main() { int i,j,k,r; //declaring integer variables i,j,k for loops and r for number of rows printf("Enter the number of rows :\n"); //Asking user for input scanf("%d",&r); //saving number of rows in variable r for(i=0;i<r;i++) //outer loop for number of rows { for(k=r;k>i+1;k--)//nested loop for number of spaces { printf(" ");//printing spaces } for(j=0;j<=i*2;j++)//nested loop for printing stars { if(i==r-1) //condition to print last row { printf("*"); //printing stars in last row } else //else condition to print rest of the stars { if(j==0||j>=i*2) //condition to print stars { printf("*"); //printing stars } else //else condition to print spaces { printf(" "); //printing spaces } } } printf("\n"); //printing newline } }
TAKING INPUT:
DISPLAYING OUTPUT:
Login/Signup to comment
#include
int main()
{
int n;
printf(“Enter n value”);
scanf(“%d”, &n);
for (int i = 0; i < n-1; i++)
{
for (int j = 0; j < n – i – 1; j++)
{
printf(" ");
}
for (int z = 0; z < (i * 2) + 1; z++)
{
if (z == 0 || z == (i * 2))
{
printf("*");
}
else {
printf(" ");
}
}
printf("\n");
}
for (int i = 0; i < (n*2)-1; i++)
{
printf("*");
}
}
Join our Discord channel for technical queries.
#include
using namespace std;
int main()
{
int n;
cout<<"Enter the any number : "<>n;
for(int i=0; i<n; i++)
{
for(int j=0; j<n-1-i; j++)
{
cout<<" ";
}
for(int k=0; k<=2*i; k++)
{
if(i==0 || i==n-1 || k==0 || k==2*i)
{
cout<<"*"<<"";
}
else{
cout<<" "<<"";
}
}
cout<<endl;
}
}
C++ EASY SOLUTION
#include
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=1;ii ;k–)
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;
}
for(Int i=1;ii;k–)
{
cout<<" ";
}
—–
C++ SOLITION IN EASY WAY—
Coder- ADIL KHAN
#include
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=1;ii;k–)
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;
}
n = int(input())
k = n-1
for i in range(n):
if(i==0 or i==n-1):
print(‘ ‘*k,’*’*((2*i)+1),’ ‘*k,sep=”)
else:
print(‘ ‘*k,’*’,’ ‘*((2*i)-1),’*’,’ ‘*k,sep=”)
k-=1
/*
* = 1 4
*-* = 3 345
*—* = 5 23456
******* = 7 1234567
*/
#include
int main()
{
int row; //4
printf(“enter the number of rows : “);
scanf(“%d”,&row);
for (int i = 0; i i+1 ; j–) // spaces: 4,3,2 ; 4,3 ; 4
{
printf(” “);
}
for (int j = 0; j <= i*2 ; j++) // 0"0" ; 0"2" ; 0"4";0"6"
{
if (i==0 || j==0 || i==row-1 || j==i*2 )
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Thanks for contributing your code Kandukuri
Python 3
n=int(input())
k=2*n-2
for i in range(n):
for j in range(0,k):
print(” “,end=”)
k-=1
for j in range(0,i+1):
if (i == 0 or i == n – 1 or j == 0 or j == n – 1 or i==1 or i==j):
print(‘*’, end=”)
else:
print(‘ ‘, end=”)
print(end=’\n’)
n = 4
for i in range(n):
if i == 0 or i == n-1:
print(‘ ‘*(n -1 – i) + ‘*’ * (2*i + 1))
else:
print(‘ ‘*(n-1-i) + ‘*’+’ ‘*(2*i – 1) +’*’)
import java.io.*;
import java.lang.*;
class space
{
public static void main(String[]args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
int k=0;
for(int i=1;i<n;i++)
{
for(int j=i;j<n;j++)
{
System.out.print(" ");
}
while(k!=(2*i-1))
{
if(k==0||k==2*i-2)
System.out.print("*");
else
System.out.print(" ");
k++;
}k=0;
System.out.println(" ");
}
for(int i=0;i<2*n-1;i++)
System.out.print("*");
}
}