JAVA program print pattern of mirrored half diamond using star

JAVA program to print a pattern of mirrored half diamond using star

This JAVA program is to print a pattern of mirrored half diamond using star(*).

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

If you need a dry run of the program or any other query, then kindly leave a comment in the comment box or mail me, I would be more than happy to help you.

Program

import java.util.*;

class sp9
{
	public static void main(String args[])
	{		
		int i,j,n;
		Scanner sc = new Scanner(System.in);
    		System.out.println("Enter value of n");
    		n = sc.nextInt();
    
		for(i=1;i<=n;i++)
    		{
        		for(j=i;j<n;j++)
        		{
            			System.out.print(" ");
        		}
        		for(j=1;j<=i;j++)
        		{
            			System.out.print("*");
        		}
        		System.out.println("");
    		}	
		for(i=n;i>=1;i--)
    		{
        		for(j=i;j<=n;j++)
        		{
            			System.out.print(" ");
        		}
        		for(j=1;j<i;j++)
        		{
            			System.out.print("*");
        		}
        		System.out.println("");
    		}	
	}
}

Output

Share Me!