Simple and easy way draw any kinds of pattern c/c++ and java language program
ava Program to Print Diamond Pattern
This is a Java Program to
Print Diamond Pattern.
Enter the number of rows as an input. Now we use for loops to print two equiateral triangles facing away from each other but with same base.
Here is the source code of the Java Program to Print Diamond Pattern. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Diamond
{
public static void main(String args[])
{
int n, i, j, space = 1;
System.out.print("Enter the number of rows: ");
Scanner s = new Scanner(System.in);
n = s.nextInt();
space = n - 1;
for (j = 1; j <= n; j++)
{
for (i = 1; i <= space; i++)
{
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j <= n - 1; j++)
{
for (i = 1; i <= space; i++)
{
System.out.print(" ");
}
space++;
for (i = 1; i <= 2 * (n - j) - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
Output:
$ javac Diamond.java
$ java Diamond
Enter the number of rows: 5
*
***
*****
*******
*********
*******
*****
***
*
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
Here’s the list of Best Reference Books in Java Programming,
Data Structures and Algorithms.
« Prev Page - Java Program to
Find G.C.D of Two Numbers
» Next Page - Java Program to
Find Sum of Natural Numbers Using While Loop
Deep Dive
@ Sanfoundry:
Comments
Post a Comment