Showing posts with label Java Program Example. Show all posts
Showing posts with label Java Program Example. Show all posts

May 6, 2022

Java Code To Print Odd Numbers In A Range From 1 To 100.

 This code prints the odd numbers in a given range. 

code box with copy button

Java Code



        public class oddNumber {
            public static void main(String[] args) {
                int from = 1;
                int to = 10;
        
                for(int i = from; i <= to; i++) {
                    if(i % 2 != 0) {
                        System.out.print(i + "\n");
                    }
                }
            }
        }
        

    


Output 

1

3

5

7

9


Share:

May 3, 2022

Java Program To Print Odd Number | Value Enter By User

code box with copy button

Java Code



        import java.util.Scanner;

        public class oddNumber {
            public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
        
                int from;
                int to;
                System.out.println("Enter Number For Start Printing Odd ");
                from = sc.nextInt();
                System.out.println("Enter Number For Ending Printing Odd ");
                to = sc.nextInt();
                for (int i = from; i <= to; i++) {
                    if (i % 2 != 0) {
                        System.out.print(i + "\n");
                    }
                }
            }
        }
        
        

    

 

Output 

Enter Number For Start Printing Odd 

1

Enter Number For Ending Printing Odd 

10

1

3

5

7

9

Share: