-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created Simple Calculator in Java (#81)
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
|
||
import java.util.*; | ||
|
||
class Calculator{ | ||
|
||
public static void main(String args[]){ | ||
Scanner sc = new Scanner(System.in); | ||
|
||
System.out.println("Simple calculator."); | ||
System.out.println("\t 1. Press 1 for Addition."); | ||
System.out.println("\t 2. Press 2 for Subtraction."); | ||
System.out.println("\t 3. Press 3 for Multiplication."); | ||
System.out.println("\t 4. Press 4 for Division."); | ||
System.out.println("\t 5. Press 5 for Remainder."); | ||
System.out.println("Enter your choice: "); | ||
int choice = sc.nextInt(); | ||
|
||
int a=0,b=0,r=0; | ||
if(choice <6 && choice >0){ | ||
System.out.println("Enter 1st no. A:"); | ||
a=sc.nextInt(); | ||
System.out.println("Enter 2nd no. B:"); | ||
b=sc.nextInt(); | ||
} | ||
|
||
if(choice==1) //For Addition | ||
{ | ||
r=a+b; | ||
System.out.println("Addition Result : "+r); | ||
} | ||
else if(choice==2) //For Subtraction | ||
{ | ||
r=a-b; | ||
System.out.println("Subtraction Result : "+r); | ||
} | ||
else if(choice==3) // For Multiplication | ||
{ | ||
r=a*b; | ||
System.out.println("Multiplication Result : "+r); | ||
} | ||
else if(choice==4) // For Division | ||
{ | ||
if(b==0){ | ||
|
||
if(a==0){ | ||
|
||
System.out.println("invalid"); | ||
|
||
} | ||
else{ | ||
|
||
System.out.println("infinite"); | ||
} | ||
} | ||
else | ||
{ | ||
r=a/b; | ||
System.out.println("Division Result : "+r); | ||
} | ||
} | ||
else if(choice==5) // For Rmainder | ||
{ | ||
if(b==0){ | ||
|
||
if(a==0){ | ||
|
||
System.out.println("invalid"); | ||
|
||
} | ||
else{ | ||
|
||
System.out.println("infinite"); | ||
} | ||
} | ||
else | ||
{ | ||
r=a%b; | ||
System.out.println("Remainder: "+r); | ||
} | ||
|
||
} | ||
else{ | ||
System.out.println("invalid choice"); | ||
} | ||
} | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.