-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
178 lines (151 loc) · 4.82 KB
/
Main.java
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.util.Scanner;
public class Main {
static Scanner scanner;
static void addition() {
int number, result = 0;
while (true) {
System.out.print("Number: ");
number = scanner.nextInt();
if (number == 0) {
break;
}
result += number;
}
System.out.println("Result: " + result);
}
static void subtraction() {
System.out.print("How many numbers will you enter: ");
int counter = scanner.nextInt();
int number, result = 0;
for (int i = 1; i <= counter; i++) {
System.out.print(i + ". number: ");
number = scanner.nextInt();
if (i == 1) {
result += number;
continue;
}
result -= number;
}
System.out.println("Result: " + result);
}
static void multiplication() {
int number, result = 1;
while (true) {
System.out.print("Number: ");
number = scanner.nextInt();
if (number == 1)
break;
if (number == 0) {
result = 0;
break;
}
result *= number;
}
System.out.println("Result: " + result);
}
static void division() {
System.out.print("How many numbers will you enter: ");
int counter = scanner.nextInt();
double number, result = 0.0;
for (int i = 1; i <= counter; i++) {
System.out.print(i + ". number: ");
number = scanner.nextDouble();
if (i != 1 && number == 0) {
System.out.println("You cannot enter 0 as divisor.");
continue;
}
if (i == 1) {
result = number;
continue;
}
result /= number;
}
System.out.println("Result: " + result);
}
static void power() {
System.out.print("Enter the base value: ");
int base = scanner.nextInt();
System.out.print("Enter the exponent value: ");
int exponent = scanner.nextInt();
int result = 1;
for (int i = 1; i <= exponent; i++) {
result *= base;
}
System.out.println("Result: " + result);
}
static void factorial() {
System.out.print("Enter a number: ");
int n = scanner.nextInt();
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
System.out.println("Result: " + result);
}
static void modulus() {
System.out.print("Enter the dividend: ");
int dividend = scanner.nextInt();
System.out.print("Enter the divisor: ");
int divisor = scanner.nextInt();
int result = dividend % divisor;
System.out.println("Result: " + result);
}
static void rectangle() {
System.out.print("Enter the length of the rectangle: ");
int length = scanner.nextInt();
System.out.print("Enter the width of the rectangle: ");
int width = scanner.nextInt();
int area = length * width;
int perimeter = 2 * (length + width);
System.out.println("Rectangle Area: " + area);
System.out.println("Rectangle Perimeter: " + perimeter);
}
public static void main(String[] args) {
scanner = new Scanner(System.in);
int select;
String menu = "1- Addition\n"
+ "2- Subtraction\n"
+ "3- Multiplication\n"
+ "4- Division\n"
+ "5- Power Calculation\n"
+ "6- Factorial Calculation\n"
+ "7- Modulus\n"
+ "8- Rectangle Area and Perimeter Calculation\n"
+ "0- Exit";
do {
System.out.println(menu);
System.out.print("Please select an operation: ");
select = scanner.nextInt();
switch (select) {
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiplication();
break;
case 4:
division();
break;
case 5:
power();
break;
case 6:
factorial();
break;
case 7:
modulus();
break;
case 8:
rectangle();
break;
case 0:
break;
default:
System.out.println("You entered an incorrect value, please try again.");
}
} while (select != 0);
}
}