SIMPLE CALCULATOR
JAVA CODING
- ELEMENTS OF JAVA USED IN PROGRAMMING -:
1.) double ---> It is a data type in java that can store decimal numbers. The decimals number in this data type can store numbers up to 15 decimal places.
3.) import --->It is a statement in java that helps the user to import a particular set of files (pre-installed in java) to help the user to access different features in java.
4.) if else STATEMENT---> They are the conditional statements that are preferred to execute a particular statement when an number of possible paths are available for the program to be executed.
5.) (a) switch STATEMENT--->It is the statement that executes a single code out according to the condition specified by the user, out of a number of possible executable codes.
(b) break STATEMENT---> When the program finds an appropriate statement according to the user's input, and if and after the statement "break" is written then from that point the program doesn't checks any other case. This helps to save time and the work becomes easy.
6.) System.out.println()----->This command helps the user to print a particular value or displays a particular enclosed statement.
- NOTE-:
- CASE of letter must not be ignored.
- Anyone not having idea of switch statement should check it as stated below.
- This program is a user input program.
- After end of every statement remember to close it with semicolon sign i.e. ;
- none of the special character should be ignored.
- PROGRAMING-:
package comnditionalstatement;
import java.util.Scanner;
public class Calculator {
public static void main (String[]args) {
Scanner sc= new Scanner (System.in);
System.out.println("Enter the first number");
double firstNum = sc.nextFloat();
System.out.println("Enter the second number");
double secondNum= sc.nextFloat();
System.out.println("Enter the sign of the operator ( +, - , * , / ) to be used");
char operator = sc.next().charAt(0);
double result = 0;
switch (operator) {
case '+':
result = firstNum+secondNum;
break;
case '-':
result = firstNum-secondNum;
break;
case '/':
result = firstNum/secondNum;
break;
case '*':
result = firstNum*secondNum;
break;
}
if (operator == '+' || operator == '-' || operator == '/' || operator == '*' ) {
System.out.println("the result of the operation performed is " + result );
}
else {
System.out.println("Error the operation performed is not valid");
}
sc.close();
}
}
No comments:
Post a Comment