Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
389 views

Java Program For Simple Calculator

This Java program uses a switch statement to build a basic calculator that takes numeric user input, an operator, and performs the corresponding arithmetic operation - addition, subtraction, multiplication, or division - to calculate and output the result. It imports necessary libraries, declares variables to store the numbers and operation, uses a Scanner to get user input, runs the input through the switch statement, and prints the final result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
389 views

Java Program For Simple Calculator

This Java program uses a switch statement to build a basic calculator that takes numeric user input, an operator, and performs the corresponding arithmetic operation - addition, subtraction, multiplication, or division - to calculate and output the result. It imports necessary libraries, declares variables to store the numbers and operation, uses a Scanner to get user input, runs the input through the switch statement, and prints the final result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

// Java program for simple calculator

import java.io.*;

import java.lang.*;

import java.lang.Math;

import java.util.Scanner;

public class Main {

public static void main(String[] args)

// stores two numbers

double num1, num2;

// Take input from the user

Scanner sc = new Scanner(System.in);

System.out.println("Enter the numbers");

// take the inputs

num1 = sc.nextDouble();

num2 = sc.nextDouble();

System.out.println("Enter the operator (+,-,*,/)");

char op = sc.next().charAt(0);

double o = 0;
switch (op) {

// case to add two numbers

case '+':

o = num1 + num2;

break;

// case to subtract two numbers

case '-':

o = num1 - num2;

break;

// case to multiply two numbers

case '*':

o = num1 * num2;

break;

// case to divide two numbers

case '/':

o = num1 / num2;

break;
default:

System.out.println("You enter wrong input");

break;

System.out.println("The final result:");

System.out.println();

// print the final result

System.out.println(num1 + " " + op + " " + num2

+ " = " + o);

You might also like