Search Java Programs

Monday, January 11, 2010

Triangle Printing - Java Program

public class TrianglePrinting {

/*
* Prints the numbers in the right angle manner.
*/

public static void TrianglePrinting(int n) {
int counter = 0, i = 1;
while (i <= n) {
counter++;
for (int j = 0; j < counter; j++) {
if (i > n)
break;
System.out.print(i + " ");
i++;
}
System.out.println();
}
}

public static void main(String[] args) {
TrianglePrinting(14);
}

}
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14

No comments:

Post a Comment

Website Design by Mayuri Multimedia