Example
Input
6
Output
1 3 5
Required to know
Use of Variables, Use of different operators like assignment or arithmetic operators, Use of methods like System. out. print(), and use for loop, and Use of Data types.
Steps
Take range as input from the user.
Run the loop by providing a condition that the loop will go on until it is more than the range. Now inside the loop provide another condition that if the number is not divisible by 2, it needs to print the odd number.
Program
Case 1: - This is when we use the if condition to print the odd number.
import java.util.Scanner;
public class Mavenproject1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
System.out.print("Enter the number");
n = sc.nextInt();
for(int i=1;i<=n;i++){
if(i%2!=0){
System.out.print(+i);
}
}
}
}
Case 2: - This is when we use only the for loop to print the odd number.
import java.util.Scanner;
public class Mavenproject1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
System.out.print("Enter the number");
n = sc.nextInt();
for(int i=1;i<=n;i=i+2){
System.out.print(i+ " ");
}
}
}