0% found this document useful (0 votes)
4 views

Screen Shot 4

Uploaded by

R7D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Screen Shot 4

Uploaded by

R7D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1 package project;

2 import java.util.Scanner;
3
4 public class Project {
5
6
7 public static void main(String[]
(String[] args) {
8 Scanner input = new Scanner(System.in);
Scanner(System.
9 String choice;
10
11 System.out.println("
.println(" Calculator ");
12 System.out.println("=============================");
.println("=============================");
13 System.out.println("|
.println("| x a^b a%b !a |");
14 System.out.println("|
.println("| 7 8 9 * |");
15 System.out.println("|
.println("| 4 5 6 ÷ |");
16 System.out.println("|
.println("| 1 2 3 + |");
17 System.out.println("|
.println("| 0 |x| log(x) - |");
18 System.out.println("|
.println("| avg(a,b,...) sin(x) = |");
19 System.out.println("=============================");
.println("=============================");
20 System.out.println();
System. .println();
21
22 do {
23 System.out.println();
System. .println();
24 System.out.printf("|
System. .printf("| %-2s | %-36s |\n",
| ", "Numper", "Operation");
25 System.out.println("==============================================");
System. .println("==============================================");
26 System.out.printf("|
System. .printf("| %-2s | %-40s |\n",
| ", "1", "Addition of two numbers");
27 System.out.printf("|
System. .printf("| %-2s | %-40s |\n",
| ", "2", "Subtraction of two numbers");
28 System.out.printf("|
System. .printf("| %-2s | %-40s |\n",
| ", "3", "Multiplication of two numbers");
29 System.out.printf("|
System. .printf("| %-2s | %-40s |\n",
| ", "4", "Division of two numbers");
30 System.out.printf("|
System. .printf("| %-2s | %-40s |\n",
| ", "5", "Modulus (a % b)");
31 System.out.printf("|
System. .printf("| %-2s | %-40s |\n",
| ", "6", "Power (a^b)");
32 System.out.printf("|
System. .printf("| %-2s | %-40s |\n",
| ", "7", "Square root of x");
33 System.out.printf("|
System. .printf("| %-2s | %-40s |\n",
| ", "8", "Factorial of a number");
34 System.out.printf("|
System. .printf("| %-2s | %-40s |\n",
| ", "9", "Log(n)");
35 System.out.printf("|
System. .printf("| %-2s | %-40s |\n",
| ", "10", "Sin(x)");
36 System.out.printf("|
System. .printf("| %-2s | %-40s |\n",
| ", "11", "Absolute value (|x|)");
37 System.out.printf("|
System. .printf("| %-2s | %-40s |\n",
| ", "12", "Average of numbers in array");
38 System.out.printf("|
System. .printf("| %-2s | %-40s |\n",
| ", "0", "Close");
39 System.out.println("==============================================");
System. .println("==============================================");
40
41 choice = input.next();
42
43 switch (choice) {
44 case "1":
45 addition(input);
46 break;
47 case "2":
48 subtraction(input);
49 break;
50 case "3":
51 multiplication(input);
52 break;
53 case "4":
54 division(input);
55 break;
56 case "5":
57 modulus(input);
58 break;
59 case "6":
60 power(input);
61 break;
62 case "7":
63 squareRoot(input);
64 break;
65 case "8":
66 factorial(input);
67 break;
68 case "9":
69 log(input);
70 break;
71 case "10":
72 sin(input);
73 break;
74 case "11":
75 absolute(input);
76 break;
77 case "12":
78 average(input);
79 break;
80 case "0":
81 System.out.println("!Turn
System. .println("!Turn Off!");
82 break;
83 default:
84 System.out.println("Invalid
System. .println("Invalid choice.");
85 break;
86 }
87 } while (!choice.equals("0"));
88
89
90 }
91
92 public static void addition(Scanner
(Scanner input) {
93 try {
94 System.out.print("Enter
System. .print("Enter the first numper : ");
95 double num1 = input.nextDouble();
96 System.out.print("Enter
System. .print("Enter the second numper : ");
97 double num2 = input.nextDouble();
98 System.out.println("Result:
System. .println("Result: " + (num1 + num2));
99 } catch (Exception e) {
100 System.out.println("Invalid
System. .println("Invalid input for addition.");
101
102 }
103 }
104
105 public static void subtraction(Scanner
(Scanner input) {
106 try {
107 System.out.print("Enter
System. .print("Enter the first numper : ");
108 double num1 = input.nextDouble();
109 System.out.print("Enter
System. .print("Enter the second numper : ");
110 double num2 = input.nextDouble();
111 System.out.println("Result:
System. .println("Result: " + (num1 - num2));
112 } catch (Exception e) {
113 System.out.println("Invalid
System. .println("Invalid input for subtraction.");
114
115 }
116 }
117
118 public static void multiplication(Scanner
(Scanner input) {
119 try {
120 System.out.print("Enter
System. .print("Enter the first numper : ");
121 double num1 = input.nextDouble();
122 System.out.print("Enter
System. .print("Enter the second numper : ");
123 double num2 = input.nextDouble();
124 System.out.println("Result:
System. .println("Result: " + (num1 * num2));
125 } catch (Exception e) {
126 System.out.println("Invalid
System. .println("Invalid input for multiplication.");
127
128 }
129 }
130
131 public static void division(Scanner
(Scanner input) {
132 try {
133 System.out.print("Enter
System. .print("Enter the Numerator: ");
134 double Numerator = input.nextDouble();
135 System.out.println("Enter
System. .println("Enter the Denominator");
136 double Denominator = input.nextDouble();
137 if (Denominator != 0) {
138 System.out.println("Result:
System. .println("Result: " + (Numerator / Denominator));
139 } else {
140 System.out.println("Cannot
System. .println("Cannot divide by zero.");
141 }
142 } catch (Exception e) {
143 System.out.println("Invalid
System. .println("Invalid input for division.");
144
145 }
146 }
147
148 public static void modulus(Scanner
(Scanner input) {
149 try {
150 System.out.print("Enter
System. .print("Enter the first numper : ");
151 int num1 = input.nextInt();
152 System.out.print("Enter
System. .print("Enter the second numper : ");
153 int num2 = input.nextInt();
154 System.out.println("Result:
System. .println("Result: " + (num1 % num2));
155 } catch (Exception e) {
156 System.out.println("Invalid
System. .println("Invalid input for modulus operation.");
157
158 }
159 }
160
161 public static void power(Scanner
(Scanner input) {
162 try {
163 System.out.print("Enter
System. .print("Enter the Base : ");
164 double Base = input.nextDouble();
165 System.out.print("Enter
System. .print("Enter the Exponent : ");
166 double Exponent = input.nextDouble();
167 System.out.println("Result:
System. .println("Result: " + Math.pow(Base,
Math. (Base, Exponent));
168 } catch (Exception e) {
169 System.out.println("Invalid
System. .println("Invalid input for power operation.");
170
171 }
172 }
173
174 public static void squareRoot(Scanner
(Scanner input) {
175 try {
176 System.out.print("Enter
System. .print("Enter the number: ");
177 double num = input.nextDouble();
178 if (num >= 0) {
179 System.out.println("Result:
System. .println("Result: " + Math.sqrt(num));
Math.
180 } else {
181 System.out.println("Cannot
System. .println("Cannot calculate square root of a negative number.");
182 }
183 } catch (Exception e) {
184 System.out.println("Invalid
System. .println("Invalid input for square root operation.");
185
186 }
187 }
188
189 public static void factorial(Scanner
(Scanner input) {
190 try {
191 System.out.print("Enter
System. .print("Enter an integer: ");
192 int num = (int)input.nextDouble();
193
194 if (num >= 0) {
195 long result = 1;
196 for (int i = 2; i <= num; i++) {
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

You might also like