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++).
Use an if condition to to print the top half of the pyramid. if (i<=r/2). Then run a loop from j=0 to j<=i. The loop should be structured as for(j=0 ; j<=i ; j++)
Inside this loop print star.
Else run a different loop from j=i to j<r. The loop should be structured as for( j=i ; j<r ; j++).
Inside this loop print star.
Inside the main loop print a newline to move to the next line after each row is printed.
CODE IN C:
#include<stdio.h>
int main()
{
int i,j,r; //declaring integer variables i,j for loops and r for number of rows
printf("Enter the number of rows(odd) :\n"); //Asking user for input
scanf("%d",&r); //taking number of rows and saving it in variable r
for(i=0;i<r;i++) // loop for number of rows
{
if(i<=(r/2)) //if condition to print the top half
{
for(j=0;j<=i;j++) // loop for stars per each row
{
printf("*"); //printing stars
}
}
else //else condition to print the bottom half
{
for(j=i;j<r;j++) //loop for printing
{
printf("*"); //printing stars
}
}
printf("\n"); // printing newline after each row
}
}
#python
n = int(input())
m = n//2+1
for i in range(1,m+1):
print(‘*’*i)
for j in range(m-1,0,-1):
print(‘*’*j)
n=int(input(“enter number of rows:”))
for i in range(0,n+1):
print(“*”*(i+1))
for i in range(n-1,-1,-1):
print(“*”*(i+1))
JAVA:
public class Main
{
void pattern(int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
System.out.print("*");
}
System.out.print("\n");
}
for(int k=1;k<n;k++)
{
for(int l=k;l<n;l++)
{
System.out.print("*");
}
System.out.print("\n");
}
}
public static void main(String[] args) {
int n=4;
Main s=new Main();
s.pattern(n);
}
}
code in python:
n=int(input(“enter a number:”)
o=””
for i in range(0,n):
o=”*”*(i+1)
print(o)
for i in range(0,n):
print(o[0:n-i-1])