java code for this? Write a program that prompts the user for input to produce t
ID: 3707546 • Letter: J
Question
java code for this?
Write a program that prompts the user for input to produce the output similar to what is shown below, if user input is 3. This diamond pattern is achieved through nested loop concept. There is no need to use any advanced techniques other than what we covered in class. Hint: Consider taking care of the space before you print the stars aligned as shown. Your program should be able to accept an integer n, then prints the picture of a diamond with 2 n -1 rows. 2 XXx X (2 n_1 times) 2 1 0Explanation / Answer
import java.util.*;
class DiamondPattern
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
//get the input from the userr
int count;
//declare the count variable as n-1
count=n-1;
int k,i;
for (k = 1; k <= n; k++)
{
for (i = 1; i <= count; i++)
System.out.print(" ");
//run the loop to print the necessary space
count--;
for (i = 1; i <= 2 *k - 1; i++)
System.out.print("*");
//run the loop till 2*k-1 to prrint the star
System.out.println();
}// this loop is for upper triangle
count = 1;
// and this loop is for lower triangle
for (k = 1; k <= n - 1; k++)
{
for (i = 1; i <= count; i++)
System.out.print(" ");
count++;
//same as abouve here we are incrementing the counter variable
for (i = 1 ; i <= 2 *(n - k)- 1; i++)
System.out.print("*");
System.out.println();
}
}
}
// finally the diamond shaped pattern is printed
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.