Powered by Blogger.

Simple java programs to generate a Triangle and Invert triangle

/*Write a program to generate a Triangle.
  eg:
  1
  2 2
  3 3 3
  4 4 4 4 and so on as per user given number */

class Triangle{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);
          for(int i=1;i<=num;i++){
             for(int j=1;j<=i;j++){
                System.out.print(" "+i+" ");
             }
             System.out.print("\n");
           } 
    }
}



/* Write a program to Display Invert Triangle.
   Example:
          Input - 5
          Output :
          5 5 5 5 5
          4 4 4 4
          3 3 3
          2 2
          1
*/
class InvertTriangle{
      public static void main(String args[]){
           int num = Integer.parseInt(args[0]);
           while(num > 0){
              for(int j=1;j<=num;j++){
                  System.out.print(" "+num+" ");
                }
                System.out.print("\n");
                num--;
            }
      }
}
Share on Google Plus

About Unknown

Author is a Tech savvy and Web Enthusiast by nature and really love to help users by providing how-to posts and tech tutorials. He is a founder and author of technolamp.co.in and programming9.com. YouTube Channel to SUBSCRIBE:
    https://www.youtube.com/user/nvrajasekhar
Available: info@programming9.com
    Blogger Comment
    Facebook Comment

0 comments: