Software & Finance





Java - Pattern of Numbers and Stars





Here is the Java Source code to display a Pattern of Numbers and Stars.

 

 

Source Code


import java.io.*;

import java.lang.*;

 

 

class Pattern {  

 

    public static void main(String[] args) {

 

            String inpstring = "";

            InputStreamReader input = new InputStreamReader(System.in);

            BufferedReader reader = new BufferedReader(input);

 

            try

            {

                  System.out.print("Enter a Number to build pattern:");

                  inpstring = reader.readLine();

                  int n = Integer.parseInt(inpstring, 10);

 

                  for(int i = n; i > 1; i--)

                  {

                        for(int j = 1; j <= i; j++)

                              System.out.print("*");

                        System.out.println();

                  }

 

                  for(int i = 1; i <= n; i++)

                  {

                        for(int j = 1; j <= i; j++)

                              System.out.print("*");

                        System.out.println();

                  }

                  System.out.println();

 

                  for(int i = 1; i < n; i++)

                  {

                        for(int j = 1; j <= i; j++)

                              System.out.print(j);

                        System.out.println();

                  }

 

                  for (int i = n; i >= 0; i--)

                  {

                        for (int j = 1; j <= i; j++)

                              System.out.print(j);

                        System.out.println();

                  }

                  System.out.println();

            }

            catch (Exception e)

            {

                  e.printStackTrace();

            }

    }

}

 

 

 

Output


D:\Program Files\Java\jdk1.6.0_23\bin>Java Pattern

Enter a Number to build pattern:7

*******

******

*****

****

***

**

*

**

***

****

*****

******

*******

 

1

12

123

1234

12345

123456

1234567

123456

12345

1234

123

12

1