Posts

Showing posts with the label java

A simple calculator using switch statements in java

 import java.util.Scanner; //import javax.swing.JOptionPane; class Inputs{ int num1,num2; int add() { return num1+num2; } int subtract() { return num1-num2; } int multiplication() { return num1*num2; } int division() { try { return num1/num2; } catch(ArithmeticException e) { System.out.println("cannot divide by zero"); } return 0; } void setNum(int x,int y) { num1=x; num2=y; } } public class Calculator { public static void main(String[] args) { Inputs box = new Inputs(); int a,b,c,ans; Scanner obj1 = new Scanner(System.in); System.out.println("Enter first number "); a=obj1.nextInt(); System.out.println("Enter second number "); b=obj1.nextInt(); box.setNum(a, b); System.out.println("Choose 1 for addition, 2 for subtraction, 3 for multiplication & 4 for division "); c=obj1.nextInt(); switch(c) { case 1: ans=box.add(); System.out.println(...

Java program to determine greatest number among three input numbers.

 Java program to determine greatest number among three input numbers. import java.util.Scanner; public class Largest { public static void main(String[] args) { int A,B,C; Scanner obj1= new Scanner(System.in); System.out.println("Enter the first number: "); A=obj1.nextInt(); System.out.println("Enter the second number: "); B=obj1.nextInt(); System.out.println("Enter the third number: "); C=obj1.nextInt(); if(A>B && A>C){ System.out.println("A is the greatest number"); } else if(B>A && B>C) { System.out.println("B is the greatest number"); } else { System.out.println("C is the greates number"); } obj1.close(); } } Output: Enter the first number:  19 Enter the second number:  20 Enter the third number:  23 C is the greatest number