Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class NumberPyramid {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int num = Integer.parseInt(scanner.nextLine());
- // save current num and track
- int currentNum = 1;
- for(int r = 1; r <= num; r++) {
- //check for value of current num
- boolean isCurrentNum = false;
- for (int c = 1; c <= r; c++) {
- // print the current num and increment for next step
- System.out.print(currentNum + " ");
- currentNum++;
- // check if current num is greater than the num above
- if(currentNum > num) {
- // set the flag on true
- isCurrentNum = true;
- break;
- }
- }
- if(isCurrentNum) {
- break;
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement