Java Print Triangle Example.

Hello Friends,

This is one of the basic examples for newbies who are still in the learning phase. 🙂 Most of you may get an assignment to print triangle of * or numbers or serial number etc. So, this example if for all those guys n gals. In this example we will print a Right angle triangle where we will print the sequence something like:

1
23
456
78910

Following is the simple code for that:

[java]

package com.sample;

class Triangle{
public static void main(String[] args)
{
try{
int a = 1;
for (int i=1; i<5;i++ )
{
for (int j=1; j<=i;j++ )
{
System.out.print(a);
a=a+1;
}
System.out.println(“”);
}
}
catch(Exception e){
System.out.println(“Exception “+e);
}
}
}
[/java]

Hope this will help you.

Regards,

Nikhil Naoghare

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.