Java program to check given number is odd, even, or prime using switch case.

visit:- allinpython.com  for more practicals
In this program, you should learn how to check whether the given number is Odd, Even, or Prime using a switch case and also learn how to print all the prime numbers between 1 to n (all in one program). this program helps you to learn a lot more about switch case. So before writing a program, you should know about the syntax of the switch case, how the switch case actually works, and most important what is even, odd, or prime number is.

Syntax of switch case:


switch (key) {
    case value:
        
        break;

    default:
        break;
}

Odd number: A number that is not divisible by 2 is known as an odd number.

  • Example: 1,3,5,7,9,11,13,15,17,19,21,23,25….

Even number: A number that is divisible by 2 is known as an even number.

  • Example: 2,4,6,8,10,12,14,16,18,20,22,24….

 Prime number: a number that is only divisible by 1 and itself is known as a prime number.

  • Example: 2,3,5,7,11,13,17,23,29,31,37….

Before writing this program you should also know about:

  • Arithmetic operators (+, -, *, /, %).
  • Relational operators.
  • Loops (In this program we use for-loop).
  • Break keyword.
  • Switch case (most important for this program).

Now you understand all the basic concepts to write this program, so now we write our program to check given number is odd, even, or prime using a switch case.

SOURCE CODE:


import java.util.Scanner;

public class all_number {
    public static void main(String[] args) {
        Scanner SC = new Scanner(System.in);
        System.out.print("enter a number : ");
        int n,ch,i,j,tem = 0;
        n = SC.nextInt();
        System.out.println("\nENTER 1 FOR CHECK EVEN OR ODD NUMBER");
        System.out.println("ENTER 2 FOR CHECK PRIME NUMBER");
        System.out.println("ENTER 3 FOR TO GET ALL PRIME NUMBER BETWEEN 1 TO "+n);
        System.out.print("\npls enter your choose :");
        ch = SC.nextInt();
        switch(ch){
            case 1:
           if(n%2 == 0){
               System.out.print(n+" is even number");
           }
           else{
            System.out.print(n+" is odd number");

           }
           break;
           case 2:
           for(i=2i<ni++){
               if(n%i == 0){
                     tem = 1;
               }
           }
           if(tem==1){
            System.out.print(n+" is not prime number");
           }
           else{
            System.out.print(n+" is prime number");
           }
           break;
           case 3:
           System.out.println("\nPRIME NUMBER BETWEEN 1 TO "+n);
           for(i=1;i<=n;i++){
               for(j=2;j<=n;j++){
                    if(i%j == 0){
                        break;
                    }
               }
               if(i == j){
                   System.out.print(i + " ");
               }
           }
           break;
           default:
           System.out.println("enter valid number for given operation");

        }
    }
    
}

in the third line "all number" is a file name, remember that in java after 'public class' always write your file name.

OUTPUT:

enter a number: 35

ENTER 1 FOR CHECK EVEN OR ODD NUMBER
ENTER 2 FOR CHECK PRIME NUMBER
ENTER 3 FOR TO GET ALL PRIME NUMBER BETWEEN 1 TO 35

pls enter your choose :1
35 is odd number

OUTPUT:

enter a number : 23

ENTER 1 FOR CHECK EVEN OR ODD NUMBER
ENTER 2 FOR CHECK PRIME NUMBER
ENTER 3 FOR TO GET ALL PRIME NUMBER BETWEEN 1 TO 23

pls enter your choose :2
23 is a prime number

OUTPUT:

enter a number : 24

ENTER 1 FOR CHECK EVEN OR ODD NUMBER
ENTER 2 FOR CHECK PRIME NUMBER
ENTER 3 FOR TO GET ALL PRIME NUMBER BETWEEN 1 TO 24

pls enter your choose :1
24 is even number

OUTPUT:

enter a number : 25

ENTER 1 FOR CHECK EVEN OR ODD NUMBER
ENTER 2 FOR CHECK PRIME NUMBER
ENTER 3 FOR TO GET ALL PRIME NUMBER BETWEEN 1 TO 25

pls enter your choose :3

PRIME NUMBER BETWEEN 1 TO 25
2 3 5 7 11 13 17 19 23

____xxxxx____


buy the best designer Kurti:https://amzn.to/3dDpUWY

buy Samsung Galaxy M32  https://amzn.to/3ynlwU8

buy best selling shirts for man  https://amzn.to/3Aq0A0o

buy bethe st keyboard and mouse for your PC: https://amzn.to/2VqyaDo

buy the best Smart Watchhttps://amzn.to/3yr6e0B

0 Comments