Saturday, 19 September 2020

CALCULATOR WITH JAVA

 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. 

2.) char----->It is a data type in java that stores a single character or letter in java. 

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-:

  1. CASE of letter must not be ignored.
  2. Anyone not having idea of switch statement should check it as stated below.
  3. This program is a user input program.
  4. After end of every statement remember to close it with semicolon sign i.e. ;
  5. 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();

}

}

NOTE-: The set-up file that we import here is Scanner file and the name of the set-up file is "java.util.Scanner" it is a type of set-up file that helps user to input data.


  • PROGRAMING IMAGE-:

IMAGE-1




IMAGE-2 



NOTE-: Image how to use a switch statement-:



  • INPUT AND OUTPUT IMAGE IN CONSOLE-:
OUTPUT-1




 OUTPUT-2



THANK YOU 😌

FOR VISITING 

  PLEASE DO FOLLOW OUR PAGE👱








No comments:

Post a Comment

Popular