In this section, we will develop a Java program to display odd numbers from 1 to 100. Understanding the Java odd number program requires a basic knowledge of Java for loop and if statements.
There are various ways to display odd numbers in Java:
- Using a Java for Loop
- Utilizing a nested-if Statement
- Employing a while Loop
Display Odd Numbers Using Java for Loop
In the following example, we define a variable called “number” and initialize it with the value 100, which represents the limit for printing odd numbers. The program uses a for loop that iterates 100 times, incrementing the variable “i” with each iteration. Within the loop, an if statement checks whether the current value of “i” is an odd number. This is done by dividing “i” by 2 and checking if the remainder is not zero. If the remainder is not zero, the number is considered odd, and the program prints it. After printing each odd number, the value of “i” is increased by 1.
By employing this approach, we can identify and print all the odd numbers within the given range.
DisplayOddNumbersExample.java
public class DisplayOddNumbersExample { public static void main(String args[]) { int number=100; System.out.print("List of odd numbers from 1 to "+number+": "); for (int i=1; i<=number; i++) { //logic to check if the number is odd or not //if i%2 is not equal to zero, the number is odd if (i%2!=0) { System.out.print(i + " "); } } } }
Output:
List of odd numbers from 1 to 100: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
Display Odd Numbers Using nested-if Statement
The following program differs slightly from the previous one as it includes a method that encapsulates the logic to check for odd numbers. Inside this method, a nested-if statement is used.
DisplayOddNumbersExample1.java
public class DisplayOddnumbersExample1 { public static void main(String[] args) { System.out.println("List of odd numbers: "); //method calling displayOddNumbers(1, 100); } //method that checks the number is odd or not private static void displayOddNumbers(int number, int end) { if(number>end) return; if(number%2!=0) { //prints the odd numbers System.out.print(number +" "); //calling the method and increments the number by 2 if the number is odd displayOddNumbers(number + 2, end); } else { //increments the number by 1 if the number is odd displayOddNumbers(number + 1, end); } } }
Output:
List of odd numbers: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
Display Even Numbers Using while Loop
In the following program, we have modified it by replacing the for loop with a while loop.
DisplayOddNumbersExample2.java
import java.util.Scanner; public class DisplayOddNumbersExample2 { public static void main(String[] args) { int number, i; Scanner sc=new Scanner(System.in); System.out.print("Enter the limit: "); number = sc.nextInt(); i=1; System.out.print("Lit of odd numbers: "); //the while loop executes until the cond\ition become false while(i<=number) { //prints the odd number System.out.print(i +" "); //increments the variable i by 2 i=i+2; } } }
Output:
Enter the limit: 100 Lit of odd numbers: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
In conclusion, the provided Java program effectively displays odd numbers from 1 to 100 using either a for loop or a while loop. The program checks each number within the given range and prints it if it is an odd number. By utilizing conditional statements and looping constructs, such as the if statement and while loop, the program successfully identifies and displays the odd numbers. Overall, this program demonstrates the fundamental usage of loops and conditional statements in Java to identify and display odd numbers within a specified range.
To learn more like Java Code to Display Odd Numbers From 1 to 100, follow tutorials.freshersnow.com to learn more.