# Print Half Diamond Star Pattern

*

**

***

****

***

**

*

## PREREQUISITE:

Basic knowledge of C language and use of loops.

## ALGORITHM:

1. Take the number of rows as input from the user and store it in any variable.(‘r‘ in this case).
2. 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++).
3. 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++)
4. Inside this loop print star.
5. Else run a different loop from j=i to j<r. The loop should be structured as for( j=i ; j<r ; j++).
6. Inside this loop print star.
7. 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
}
}

## DISPLAYING OUTPUT:

### 5 comments on “Print Half Diamond Star Pattern”

• Vijaykhede

public class Main {
public static void main(String[] args){
int i,j,k,c=7,r=4;
for(i=1;i<=c;i++){
if(i<=4)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
}
else
{
for(j=1;j<=r+r-i;j++)
{
System.out.print("*");
}
}
System.out.println();
}
}
}

• Arthi

#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)

• Neha

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))

• Srivamshi

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);

}
}

• sampath pendurthi

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])