Tuesday, March 23, 2010

Print Upright/Inverted Triangle Till n Terms

import java.io.*;

public class triangles
{ public static void main(String args[]) throws IOException
  { InputStreamReader read = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(read);
   
  System.out.println("Enter 1 for upright triangle, 2 for inverted triangle");
  int choice=Integer.parseInt(br.readLine());
   
  switch(choice)
  { case 1: System.out.println("Enter number");
  int x=Integer.parseInt(br.readLine());
   
  for(int i=1;i<=x;i++)
  { for(int j=1;j<=i;j++)
  { System.out.print(i+" ");
  }
  System.out.println();
  }
  break;
   
  case 2: System.out.println("Enter number");
  int y=Integer.parseInt(br.readLine());
   
  for(int i=y;i>=1;i--)
  { for(int j=i;j>=1;j--)
  { System.out.print(i+" ");
  }
  System.out.println();
  }
  break;
   
  default: System.out.println("Error, wrong choice monsieur");
  }
  }
}  

1 comment: