-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaBasics.java
419 lines (414 loc) · 13.2 KB
/
JavaBasics.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import java.util.*;
public class JavaBasics {
public static void main(String[] args) {
System.out.print("1,2,3,6,5,");
System.out.print("1,2,3,6,5,");
System.out.print("1,2,3,6,5,");
// }
// }
// //boilerpalte code
// public class JavaBasics{
// public static void main(String[] args){
System.out.println("*****");
System.out.println("****");
System.out.println("***");
System.out.println("**");
System.out.println("*");
// }
// }
// //VARIABLES
// public class JavaBasics{
// public static void main(String args[]){
int a = 10;
int b = 20;
System.out.println(a);
System.out.println(b);
String name = "Tony stark";
System.out.println(name);
a = b;
System.out.print(a);
// }
// }
// import java.util.*;
// public class JavaBasics{
// public static void main(String args[]){
Scanner sc = new Scanner(System.in);
//String input = sc.next();
String input = sc.nextLine();
System.out.print(input);
// }
// }
// import java.util.*;
// public class JavaBasics{
// public static void main(String args[]){
// Scanner sc = new Scanner(System.in);
int ab = sc.nextInt();
System.out.println(ab);
// }
// } /
// import java.util.*;
// public class JavaBasics{
// public static void main(String args[]){
// Scanner sc = new Scanner(System.in);
int abc = sc.nextInt();
System.out.print("The value of a is: " + abc);
// }
// }
// import java.util.*;
// public class JavaBasics{
// public static void main(String args []){
// Scanner sc = new Scanner(System.in);
float f = sc.nextFloat();
System.out.println(f);
// }
// }
// //SUM OF TWO NOS
// import java.util.*;
// public class JavaBasics{
// public static void main(String args []){
// Scanner sc = new Scanner(System.in);
int az = sc.nextInt();
int bz = sc.nextInt();
int sum = az + bz;
System.out.println(sum);
// }
// }
// // PRODUCT of 2 nos
// import java.util.*;
// public class JavaBasics{
// public static void main(String args[]){
// Scanner sc = new Scanner(System.in);
int ay = sc.nextInt();
int by = sc.nextInt();
int product = ay * by;
System.out.println(product);
// }
// }
// // ARES OF CIRCLE
// import java.util.*;
// public class JavaBasics{
// public static void main(String args []){
// Scanner sc = new Scanner(System.in);
float r = sc.nextFloat();
float ar = 3.14f * r * r;
System.out.println(ar);
// }
// }
// public class JavaBasics {
// public static void main(String args[]){
// }
// }
// /* TYPE CONVERSION ANd TYPE CATSING */
// import java.util.*;
// public class JavaBasics{
// public static void main(String args []){
int ax = 45;
float bx = 25.96f;
float c = ax;
int d = (int) bx;
char ch = 'a';
char ch2 = 'b';
int number = ch;
int number2 = ch2;
System.out.println(ch);
System.out.println(ch2);
System.out.println(number);
System.out.println(number2);
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
// }
// }
// public class JavaBasics{
// public static void main(String args []){
int $ = 24;
System.out.println($);
// }
// }
// /* Question 1 : In a program, input 3 numbers:A,BandC.
// You have to output the average of these 3 numbers.
// (Hint : Average of N numbers is sum of those numbers divided by N) */
// import java.util.*;
// public class JavaBacics{
// public static void main(String args []){
// Scanner sc = new Scanner(System.in);
System.out.print("Enter 1st no: ");
int ai = sc.nextInt();
System.out.print("Enter 2nd no: ");
int bi = sc.nextInt();
System.out.print("Enter 3rd no: ");
int ci = sc.nextInt();
float av = (ai + bi + ci) / 3;
System.out.println(av);
// }
// }
// /* Question 2 : In a program, input the side of a square.
// You have to output the area of the square.
// (Hint : area of a square is (side x side)) */
// import java.util.*;
// public class JavaBasics{
// public static void main(String args[]){
// Scanner sc = new Scanner(System.in);
System.out.print("Enter sides of Square: ");
float s = sc.nextFloat();
float Area = s * s;
System.out.println(Area);
// }
// }
// /* Question 3 : Enter cost of 3 items from the user (using float data type)
// -a pencil, a pen and an eraser. You have to output the total cost of the items
// back to the user as their bill.(Add on : You can also try adding 18% gst tax to
// the items in the bill as an advanced problem */
// import java.util.*;
// public class JavaBasics{
// public static void main (String args []){
// Scanner sc = new Scanner(System.in);
System.out.print("Enter the cost of pencil: ");
float ps = sc.nextFloat();
System.out.print("Enter the cost of pen: ");
float pn = sc.nextFloat();
System.out.print("Enter the cost of Eraser: ");
float es = sc.nextFloat();
float S_Total = ps + pn + es;
float t = S_Total * 0.18f;
float bill = S_Total + t;
System.out.println("Cost of product = " + S_Total);
System.out.println("GST = " + t);
System.out.println("Total bill with GST = " + bill);
sc.close();
// }
// }*/
// /* CONDITIONAL STATEMENTS */
// public class JavaBasics {
// public static void main(String args []){
int age = 22;
if (age >= 18) {
System.out.print("adult : Drive, Vote");
} else {
System.out.print("Not Adult");
}
// }
// }
// // ODD OR EVEN
// import java.util.*;
// public class JavaBacics{
// public static void main(String srgs[]){
System.out.print("Enter a number: ");
// Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n % 2 == 0) {
System.out.println("The no is even");
} else {
System.out.println("The no is odd");
}
// }
// }
// /* Elseif */
// import java.util.*;
// public class JavaBasics{
// public static void main(String args[]){
System.out.print("ENter the age : ");
// Scanner sc = new Scanner(System.in);
float age1 = sc.nextFloat();
if (age1 >= 18 && age1 <= 65) {
System.out.print("The person is Adult");
} else if (age1 > 65) {
System.out.println("The person is Senior Citizen");
} else {
System.out.println("You are child");
}
// }
// }
// /*INCOME TAX CALCULATOR */
// import java.util.*;
// public class JavaBacics{
// public static void main(String args[]){
System.out.print("Enter the income : ");
// Scanner sc = new Scanner(System.in);
float income = sc.nextFloat();
float tax;
if (income < 500000) {
System.out.println("No Tax Applied");
} else if (income >= 500000 && income <= 1000000) {
tax = income * 0.2f;
System.out.println("Tax applied 20% of net income: " + tax);
} else {
tax = income * 0.3f;
System.out.println("Tax Applied is 30% of net income: " + tax);
}
// }
// }
// /* TERNARAY OPERATOR */
// public class JavaBacics{
// public static void main(String args[]){
int no = 45;
//Ternary Operator
String result = (no % 2 == 0) ? "Even" : "Odd";
System.out.print(result);
// }
// }
// public class JavaBacics{
// public static void main(String args[]){
int subMarks = 45;
//tertiary oprator
String result1 = (subMarks >= 33) ? "Pass" : "Fail";
System.out.println(result1);
// }
// }
// /* SWITCH CASE */
// public class JavaBacics{
// public static void main(String args[]){
int nb = 8;
switch (nb) {
case 1:
System.out.print("Samosa");
break;
case 2:
System.out.println("Momos");
break;
case 3:
System.out.println("Chocolate shake");
break;
default:
System.out.println("Kuch nhi milne wala");
}
// }
// }
// /* CALCULATOR */
// import java.util.*;
// public class JavaBasics{
// public static void main(String args[]){
// System.out.println("What you wanna do?\n +,-,*,/,%: ");
// Scanner sc = new Scanner(System.in);
char choice = sc.next().charAt(0);
int al = 4567;
int bl = 8567;
switch (choice) {
case '+':
System.out.println(al + bl);
break;
case '-':
System.out.println(al - bl);
break;
case '*':
System.out.println(al * bl);
break;
case '/':
System.out.println(bl / al);
break;
case '%':
System.out.println(bl % al);
break;
default:
System.out.println("Give a correct choice");
}
// }
// }
// /* CONDITIONAL STAEMENT QUESTIONS */
// /*Question1:
// Write a Java program to get a number from the user
// and print whether it is positive or negative */
// import java.util.*;
// public class JavaBasics{
// public static void main(String args[]){
System.out.print("Enter a number : ");
// Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if (num < 0) {
System.out.println("The no is Negative.");
} else {
System.out.println("The no is Positive.");
}
// }
// }
// /* Question2:
// Finish the following code so that it prints You have a fever if your temperature
// is above 100 and otherwise prints You don't have a fever. */
// public class JavaBasics{
// public static void main(String args[]){
double temp = 103.5;
if (temp > 100) {
System.out.print("You have a fever.");
} else {
System.out.println("You don't have a fever.");
}
// }
// }
// /* Question3:
// Write a Java program to input week number(1-7) and print day of week name using
// switch case. */
// import java.util.*;
// public class JavaBasics{
// public static void main(String args[]){
System.out.print("Enter day no of a week : ");
// Scanner sc = new Scanner(System.in);
int day = sc.nextInt();
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Enter an interger between 1-7");
}
// }
// }
// public class JavaBasics{
// public static void main(String args[]) {
int ag = 63, bg = 36;
boolean x = (ag < bg) ? true : false;
int y = (ag > bg) ? ag : bg;
System.out.println("x = " + x);
System.out.println("y : " + y);
// }
// }
// /*Question5: WRONG SOL
// Write a Java program that takes a year from the user and print whether that year
// is a leap year or not.
// WRONG SOL*/
// import java.util.*;
// public class JavaBasics{
// public static void main(String args[]){
System.out.print("Enter a year : ");
// Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
if (year % 4 == 0) {
System.out.println("This is a leap year");
} else if (year % 100 == 0) {
System.out.println("This is a leap year");
} // // WRONG SOL
else if (year % 400 == 0) {
System.out.println("This is a leap year");
} else {
System.out.println("This is NOT a leap year");
}
// }
// }
// /* LOOPS */
// public class JavaBasics{
// public static void main(String args[]){
int counter = 0;
while (counter < 3) {
System.out.println("Navnitya is a Software Developer");
counter++;
}
}
}