Computer Science, asked by TreeshaBiswas, 4 months ago

print the pattern
in DIAMOND FORM using java programming​

Attachments:

Answers

Answered by kamalrajatjoshi94
2

Answer:

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

}

}

}

Answered by anindyaadhikari13
3

Required Answer:-

Question:

  • Write a program in Java to display the diamond pattern.

Solution:

Here comes the program.

import java.util.*;

public class DiamondPattern {

 public static void main(String[] args) {

    int i, j, n;

    Scanner sc=new Scanner(System.in);

    System.out.print("Enter n - ");

    n=sc.nextInt();

    for(i=1;i<=n;i++)  {

      for(j=1;j<=n-i;j++)

        System.out.print(" ");

      for(j=1;j<=i;j++)

        System.out.print("* ");

      System.out.println();

    }

    for(i=n-1;i>=1;i--) {

      for(j=1;j<n-i;j++)

        System.out.print(" ");

      for(j=1;j<=i;j++)

        System.out.print(" *");

      System.out.println();

    }

    sc.close();

 }

}

I have divided the pattern into two parts so as to make the program easy to solve. See the attachment for output.

Attachments:
Similar questions