Answer to Question #313652 in Java | JSP | JSF for Kyle

Question #313652

Write a program in Java to display a diamond pattern of asterisks.

Expected Output :                                                                                

      *                                                                         

     ***                                                                        

    *****                                                                       

   *******                                                                      

  *********      

 ***********                   

*************                                                                   

 ***********                                                                    

  *********                                                                     

   *******                                                                      

    *****                     

     ***                                                                        

      *


1
Expert's answer
2022-03-18T08:42:48-0400
public class Diamond 
{
 
 
public static void main(String[] args)
{
 
Scanner sc=new Scanner(System.in);
System.out.println("Enter N : ");
int n=sc.nextInt(); 
System.out.print("Enter Symbol : ");
 
char c = sc.next().charAt(0);
int i=1;
int j;
while(i<=n)
{
j=1;
while(j++<=n-i)
 
{
System.out.print(" ");
 
}
j=1;
while(j++<=i*2-1)
 
{
System.out.print(c);
 
}
 
System.out.println();
i++;
} 
i=n-1;
while(i>0)
{
j=1;
while(j++<=n-i)
 
{
System.out.print(" ");
 
} 
j=1;
while(j++<=i*2-1)
 
{
System.out.print(c);
 
}
 
System.out.println();
i--;
}
 
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS