Answer to Question #289459 in Java | JSP | JSF for Kev

Question #289459

Challenging Numerical Triangles

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!


1
Expert's answer
2022-01-23T15:21:37-0500


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();
	}


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog