Java Program to Print Odd Number Pyramid

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

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:

odd number pattern program in java