write a program to print this pattern
plzz hlp I am having my test
Answers
public class DiamondPattern {
public static void main(String[ ] args) {
int columns = 3, spaces = columns - 1;
// The symbol to be printed
char symbol = '*';
// Upper half of the diamond
for (int j = 1; j <= columns; j++) {
// Printing spaces
for (int space = 1; space <= spaces; space++)
System.out.print(" ");
spaces--;
// Printing symbols
for (int i = 1; i <= 2 * j - 1; i++)
System.out.print(symbol);
System.out.println( );
}
// Reseting number of spaces
spaces = 1;
// Lower half of the diamond
for (int j = 1; j <= columns - 1; j++) {
// Printing spaces
for (int space = 1; space <= spaces; space++)
System.out.print(" ");
spaces++;
// Printing symbols
for (int i = 1; i <= 2 * (columns - j) - 1; i++)
System.out.print(symbol);
System.out.println( );
}
}
}
Question:-
Write a program to display the following pattern.
Code:-
import java.util.*;
class Pattern
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows to print this pattern: ");
int n=sc.nextInt();
for(int i=1;i<=n;i++) // upper part.
{
for(int j=1;j<=n-i;j++)
System.out.print(" ");
for(int k=1;k<=2*i-1;k++)
System.out.print("*");
System.out.println();
}
for(int i=1;i<=n-1;i++) // lower part
{
for(int j=1;j<=i;j++)
System.out.print(" ");
for(int k=1;k<=2*(n-i)-1;k++)
System.out.print("*");
System.out.println();
}
}// end of main()
}// end of class.