Author Topic: Left / Right Aligned Star Pattern  (Read 1501 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Left / Right Aligned Star Pattern
« on: June 29, 2011, 03:17:33 pm »
Here is the Java Source code using loops to display left aligned and right aligned star pattern

Pattern 1 - Left Aligned:

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

Pattern 2 - Right Aligned:

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





import java.io.*;
import java.lang.*;
import java.util.*;

class Pattern
{
   static public int ReadInteger()
   {
      try
      {
         String inpString = "";
         InputStreamReader input = new InputStreamReader(System.in);
         BufferedReader reader = new BufferedReader(input);
         String s = reader.readLine();
         return Integer.parseInt(s);
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      return -1;
   }

   public static void main(String[] args)
   {
      System.out.println("Program for displaying pattern of *.");
      System.out.print("Enter the maximum number of *: ");
      int n = ReadInteger();
      
      System.out.println("\nPattern 1 - Left Aligned:\n");
      for (int i = 1; i <= n; i++)
      {
         for (int j = 1; j <= i; j++)
            System.out.print("*");
         System.out.println();
      }

      System.out.println("\nPattern 2 - Right Aligned:\n");
      for (int i = n; i >= 1; i--)
      {
         for(int j = 0; j < n - i; j++)
            System.out.print(" ");
         for (int j = 1; j <= i; j++)
            System.out.print("*");
         System.out.println();
      }
      System.out.println();
   }
}

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1