input n and print all the odd number from 1 to n.(using for loop) java programming
Answers
import java.util.Scanner;
class Even_Oddtill1{
public static void main (String args[]){
int n;
Scanner scan=new Scanner(System.in);
//create a scanner object for input
System.out.print("Print all odd number until:\n");
int num=scan.nextInt();//Reads input from user and stored in variable num
System.out.print("Odd number from 1 to "+num+" are: \n");
for(i=1; i<=num; i++){//loop for iterate from 1 to maximum
if(i%2==1)
System.out.print(i+"\n");
}
}
}
}
★Output:
Print all odd number until:
20
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter N: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
if (i%2 == 1) {
System.out.println(i);
}
}
}
}