Monday 15 February 2021

Pattern 31 & 32

# Way 1

public class P7 
{
	public static void main(String[] args) 
	{
		int i, j, k, n=5;		
		for( i=-n; i<=n; i++)
		{
			if(i<0)
				k=-i;
			else
				k=i;
			for(j=1; j<=k; j++)
				System.out.print("  ");
			for(j=0; j<2*(n-k)+1; j++)
				System.out.print("*"+" ");
		System.out.println();
		}
	}
}

# Way 2

import java.util.Scanner;
public class P2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println(" Enter Rows : ");
		int row = sc.nextInt();
		for(int i=0; i<row; i++)
		{
			for(int j=1; j<=row-i; j++)
				System.out.print("  ");
			for(int j=1; j<=2*i-1; j++)
				System.out.print("* ");
		System.out.println();
		}
		for(int i=row; i>=0; i--)
		{
			for(int j=1; j<=row-i; j++)
				System.out.print("  ");
			for(int j=1; j<=2*i-1; j++)
				System.out.print("* ");
		System.out.println();	
		}
	}
}

OUTPUT

 	* 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 
  * * * * * * * 
    * * * * * 
      * * * 
        * 
# Way 1

import java.util.Scanner;
public class p3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print(" Enter Rows : ");
		int row = sc.nextInt();
		for(int i=row; i>=0; i--)
		{
			for(int j=1; j<=i; j++)
				System.out.print("  ");
			for(int j=1; j<=row-i; j++)
				System.out.print(" *"+"  ");
		System.out.println();	
		}
		for(int i=1; i<row; i++)
		{
			for(int j=1; j<=i; j++)
				System.out.print("  ");
			for(int j=1; j<=row-i; j++)
				System.out.print(" *"+"  ");
		System.out.println();	
		}
	}
}

# Way 2

public class P7 
{
	public static void main(String[] args) 
	{
		int i, j, k, n=5;
		for( i=-n; i<=n; i++)
		{
			if(i<0)
				k=-i;
			else
				k=i;
			for(j=1; j<=k; j++)
				System.out.print(" ");
			for(j=0; j<(n-k)+1; j++)
				System.out.print("*"+" ");
		System.out.println();
		}
	}
}

OUTPUT

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

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32