-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectOne.java
More file actions
106 lines (86 loc) · 2.32 KB
/
Copy pathprojectOne.java
File metadata and controls
106 lines (86 loc) · 2.32 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package projectOne;
/*
* Calculates even, odd and prime number.
* @author Ang
*/
import java.util.Scanner;
public class projectOne {
public static boolean isodd(int odd) {
for (int i = 0; i<odd;i++) {
if(odd%2 != 0 ) {
return true;
}
}
return false;
}
public static boolean iseven(int even) {
for (int j = 0; j<even; j++) {
if (even%2 == 0) {
return true;
}
}
return false;
}
public static boolean isprime(int prime) {
for (int k = 2; k<=prime/2; k++) {
if (prime % k == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int no;
Scanner sc = new Scanner(System.in);
System.out.println("-----WELCOME-----");
System.out.println("Please enter any number > 0:");
no = sc.nextInt();
while(no > 0) {
System.out.println("===========================");
System.out.println("Choose from the following:");
System.out.println("1. Displays odd number up to " +no+".");
System.out.println("2. Displays even number up to " +no+".");
System.out.println("3. Displays prime number up to " +no+".");
System.out.println("4. Exit");
System.out.println("===========================");
int choose = sc.nextInt();
switch(choose){
case 1:
System.out.println("*************************");
System.out.println("Odd number list:");
for (int odd = 0;odd<=no;odd++) {
if (isodd(odd)) {
System.out.println(odd);
}
}
System.out.println("**************************");
break;
case 2:
System.out.println("**************************");
System.out.println("Even number list:");
for (int even = 0;even<=no;even++) {
if (iseven(even)) {
System.out.println(even);
}
}
System.out.println("**************************");
break;
case 3:
System.out.println("*****************************");
System.out.println("Prime number list:");
for (int prime = 2;prime<=no;prime++) {
if (isprime(prime)) {
System.out.println(prime);
}
}
System.out.println("********************************");
break;
case 4:
System.out.println("Thank you, have a nice day.");
System.exit(0);
default:
break;
}
}
}
}