Search This Blog

Wednesday, 6 July 2016

Write a program to enter two numbers and perform mathematical operations on them

Unit - 1

Practical 2

package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
 
        float m,n;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter two numbers :");
        m = in.nextFloat();
        n= in.nextFloat();
        while(true)
        {
            System.out.println("Enter your choice:\n1.sum\t2.sub\t3.mul\t4.div\t5.exit : ");
            byte choice= in.nextByte();
            switch(choice)
            {
                case 1:System.out.println("Addition is :"+(m+n));
                break;
             
                case 2:System.out.println("Subtraction is :"+(m-n));
                break;
             
                case 3:System.out.println("mul is :"+(m*n));
                break;
             
                case 4:System.out.println("div is "+(m/n));
                break;
             
                case 5:System.exit(0);
                break;
             
                default:System.out.println("Enter proper choice");
                break;
             
            }
        }
   }
}


output:

Enter two numbers :
2 2
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
1
Addition is :4.0
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
2
Subtraction is :0.0
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
3
mul is :4.0
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
4
div is 1.0
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
5

No comments:

Post a Comment