File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+
3+ public class AlgebraicOperations {
4+ public static void main (String args []) {
5+
6+ //Declaring variables
7+ double n1 ,n2 ,res = 0 ;
8+
9+ System .out .println ("Algebraic Operations of two Numbers\n ---" );
10+ Scanner scanner = new Scanner (System .in );
11+
12+ System .out .println ("Enter the number 1: " );
13+ n1 = scanner .nextDouble ();
14+
15+ System .out .println ("Enter the number 2: " );
16+ n2 = scanner .nextDouble ();
17+
18+ System .out .println ("---\n Enter 1 for addition,\n 2 for subtraction\n 3 for multiplication\n 4 for division" );
19+ int operator = scanner .nextInt ();
20+
21+ //Solving by if else loops
22+ if (operator ==1 ) {
23+ res =n1 +n2 ;
24+ }
25+ else if (operator ==2 ) {
26+ res =n1 -n2 ;
27+ }
28+
29+ else if (operator ==3 ) {
30+ res =n1 *n2 ;
31+ }
32+
33+ else if (operator ==4 ) {
34+ res =n1 /n2 ;
35+ }
36+
37+ else {
38+ System .out .println ("Error!" );
39+ end ();
40+ }
41+
42+ System .out .println (res );
43+ end ();
44+ scanner .close ();
45+ }
46+
47+ static void end () {
48+ System .out .println ("Enter Y to continue or any key to exit!" );
49+
50+ Scanner getEndOption = new Scanner (System .in );
51+ String option = getEndOption .next ();
52+ if (option .equalsIgnoreCase ("Y" )) {
53+ main (null );
54+ }
55+ else {
56+ System .out .println ("The Program has ended. Please run the program again." );
57+ }
58+ getEndOption .close ();
59+ }
60+
61+
62+ }
Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+
3+ public class AlgrebraicOperationsSwitchCase {
4+ public static void main (String args []) {
5+
6+ //Declaring variables
7+ double n1 ,n2 ,res = 0 ;
8+
9+ System .out .println ("Algebraic Operations of two Numbers with Switch Case Loop\n ---" );
10+ Scanner scanner = new Scanner (System .in );
11+
12+ System .out .println ("Enter the number 1: " );
13+ n1 = scanner .nextDouble ();
14+
15+ System .out .println ("Enter the number 2: " );
16+ n2 = scanner .nextDouble ();
17+
18+ System .out .println ("---\n Enter 1 for addition or\n 2 for subtraction or\n 3 for multiplication or\n 4 for division" );
19+ int operator = scanner .nextInt ();
20+
21+ //Solving by switch case loop for simpler code.
22+ switch (operator ) {
23+ case 1 :
24+ res = n1 +n2 ;
25+ break ;
26+ case 2 :
27+ res = n1 -n2 ;
28+ break ;
29+ case 3 :
30+ res = n1 *n2 ;
31+ break ;
32+ case 4 :
33+ res = n1 /n2 ;
34+ break ;
35+ default :
36+ System .out .println ("You have chosen a wrong option.\n Please choose from the above given options." );
37+ end ();
38+ }
39+ System .out .println (res );
40+ end ();
41+ scanner .close ();
42+ }
43+
44+ static void end () {
45+ System .out .println ("---\n Enter Y to continue or any key to exit!" );
46+
47+ Scanner getEndOption = new Scanner (System .in );
48+ String option = getEndOption .next ();
49+ if (option .equalsIgnoreCase ("Y" )) {
50+ main (null );
51+ }
52+ else {
53+ System .out .println ("The Program has ended. Please run the program again." );
54+ }
55+ getEndOption .close ();
56+ }
57+
58+ }
59+
Original file line number Diff line number Diff line change 1+ class HelloWorld {
2+ public static void main (String [] args ) {
3+ System .out .println ("Hello World!!\n I'm learning Java." );
4+ }
5+ }
You can’t perform that action at this time.
0 commit comments