Q:

Java program to print ODD numbers from 1 to N

0

Java program to print ODD numbers from 1 to N

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Print Odd Numbers from 1 to N using Java Program

//Java program to print ODD numbers from 1 to N.
 
import java.util.*;
 
public class Odd{
 
    public static void main(String []args)
    {
        int n=0,i=0;
         
        Scanner X = new Scanner(System.in);
         
        System.out.print("Enter value n : ");
        n = X.nextInt();
         
        for(i=1; i<n; i++)
        {
            if(i%2!=0)
                System.out.print(i+" ");
        }   
        System.out.println();
         
    }
}

Output

    
    me@linux:~$ javac Odd.java
    me@linux:~$ java Odd

    Enter value n : 10
    1 3 5 7 9

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now