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=2; i<n; i++){ 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
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
0 Comments