In this tutorial, we will learn how to print odd number patterns in java. Before that, you may go through the following topic in java.
The program takes a user input for the number of rows and prints the number pattern of the pyramid for odd numbers in java.
Java Program to Print of Odd Number Pyramid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.util.Scanner; public class Main { public static void main(String[] args) { int i, j, k = 1, num; Scanner sc = new Scanner(System.in); System.out.print("Enter the no. of rows: "); num = sc.nextInt(); for (i = 1; i <= num; i++) { for (j = 1; j <= i; j++) { System.out.print(k + " "); k = k + 2; } System.out.print("\n"); } } } |
Output: