Wednesday, 14 October 2020

Print the following pattern according to user’s choice

543212345
5432  2345
543      345
54          45
5              5


import java.util.Scanner;
public class Pattern1
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of rows to print the pattern ");
        int rows = sc.nextInt();       
               
        for (int i = rows; i >= 1; i--)
        {
            for (int j = rows; j >= 1+rows-i; j--)
            {
                System.out.print(j);
            }
            
            for (int j= i*2 ; j < rows*2-1; j++)
            {
                System.out.print(" ");
            }
            
            for (int l = 1+rows-i; l <=rows; l++)
            {
                if(l!=1)
                    System.out.print(l);
            }
            System.out.println();
        }
       
    }
}

No comments:

Post a Comment