Print Odd Numbers up to N in Java . Java Practice Interview Programs

Print Odd Numbers up to N in Java . Java Practice Interview Programs

Print Even Numbers up to N in Java . Java Practice Interview Programs


1). Print Odd Numbers up to 100 in java.

 public class Oddnumber {   
   public static void main(String[] args) {   
    int n=100;    
    for (int i=1;i<=100;i++){   
     if (i%2!=0){   
      System.out.println(i);   
     }   
    }   
   }   
  }   


 2).Print Odd Numbers up to N By Taking User Input in java.

 import java.util.Scanner;   
  public class Oddnum {   
   public static void main(String[] args) {   
    Scanner sc=new Scanner(System.in);   
    System.out.println("Enter up to How many Number You want Odd Number?");   
    int n=sc.nextInt();   
    for (int i=1;i<=n;i++){   
     if (i%2!=0){   
      System.out.println(i);   
     }   
    }   
   }   
   }   

Post a Comment

0 Comments