Write a program that asks the user to enter a number between 5 and 10. This will
ID: 3538114 • Letter: W
Question
Write a program that asks the user to enter a number between 5 and 10. This will determine how many lines to place in the diamond. Your program will then display a pyramid similar to the one shown in the sample run below.
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
4 3 2 1 2 3 4
3 2 1 2 3
2 1 2
1
Explanation / Answer
import java.util.*;
class mukesh
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the number between 5 and 10:");
int n=s.nextInt();
for(int j=1;j<=n;j++)
{
for(int i=j-1;i<n;i++)
{
System.out.print(" ");
}
for(int k=j;k>0;k--)
{
System.out.print(k+" ");
}
for(int k=2;k<=j;k++)
{
System.out.print(k+" ");
}
System.out.println();
}
for(int j=n-1;j>0;j--)
{
for(int i=j;i<=n;i++)
{
System.out.print(" ");
}
for(int k=j;k>0;k--)
{
System.out.print(k+" ");
}
for(int k=2;k<=j;k++)
{
System.out.print(k+" ");
}
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.