by CodeChum Admin
You probably have encountered Triangle pattern problems before when you started learning programming. Let’s see how well you do with numbers this time. Try to look at the examples closely and think of an algorithm that replicates the outputs. Go for it!
import java.util.*;
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Enter n: ");
int n = keyBoard.nextInt();
if (n > 0) {
int counter = 1;
for (int i = 1; counter < n - 1; i++) {
for (int j = 1; j < i; j++) {
if (counter <= n) {
System.out.print(counter + " ");
counter++;
}
}
System.out.println();
}
}
keyBoard.close();
}
}
Comments
Leave a comment