In this post, you will learn how to print the odd numbers series using a Java program. Here, we will print a series of odd numbers up to a given number or for a specific number of terms.
Let’s see the Java program for it below:
package com.javacodepoint.series;
public class OddNumbersSeries {
public static void printOddSeries(int limit) {
System.out.print("Odd Numbers Series: ");
for (int i = 1; i <= limit; i += 2) {
System.out.print(i + " ");
}
System.out.println();
}
public static void main(String[] args) {
int limit = 10;
printOddSeries(limit);
}
}
OUTPUT:
Odd Numbers Series: 1 3 5 7 9
Explanation:
In this program, we have a method printOddSeries
that takes an integer limit
as input and prints all odd numbers from 1 up to the given limit
. The method uses a for loop with an increment of 2 (since odd numbers are spaced by 2) to generate and print the odd numbers.
See also: Java Program to Print Series 2,4,8,16,32