Repository navigation
Expand file tree
/
Copy pathAlphaBeticalOrPrimeMenuDriven.java
More file actions
58 lines (45 loc) · 1.64 KB
/
Copy pathAlphaBeticalOrPrimeMenuDriven.java
File metadata and controls
58 lines (45 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package PractisePrograms;
import java.util.Arrays;
import java.util.Scanner;
public class AlphaBeticalOrPrimeMenuDriven {
public AlphaBeticalOrPrimeMenuDriven() {
}
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter 'a' for arranging word lexicographically or 'b' for prime numbers.");
char choice = sc.next().charAt(0);
choice = Character.toLowerCase(choice);
sc.nextLine();
switch (choice) {
case 'a':
System.out.println("Enter a word:");
String word = sc.nextLine();
word = word.toUpperCase();
char[] wordch = word.toCharArray();
Arrays.sort(wordch);
String aword = new String(wordch);
System.out.println(aword);
break;
case 'b':
System.out.println("Enter a number:");
int n = sc.nextInt();
System.out.println("Prime numbers from 1 to " + n);
for(int i = 2; i <= n; ++i) {
boolean isPrime = true;
for(int j = 2; j <= i / 2; ++j) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(i);
}
}
return;
default:
System.out.println("Wrong choice");
}
}
}
}