Description
Get the number of rows from the user as print the diamond pattern using stars.
Input
4
Output
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
C Program
#include<stdio.h>
int main() {
int i, j, k, rows;
printf("Enter the row value: ");
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");
}
for(i=rows-1; i>=1; 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 the row value: ";
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";
}
for(i=rows-1; i>=1; i--)
{
for(j=i; j<rows; j++)
cout<<" ";
for(k=1; k<=(2*i -1); k++)
cout<<"*";
cout<<"\n";
}
return 0;
}
Java Program
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("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();
}
for(int i=rows-1; i>=1; 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 Program
row = int(input("Enter the number of rows: "))
for i in range(0, row):
for j in range(0, row-i-1):
print(" ",end="")
for j in range(0, i*2+1):
print("*", end="")
print()
for i in range(1, row):
for j in range(0, i):
print(" ",end="")
for j in range(0, (row-i)*2-1):
print("*", end="")
print()