Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)
I was trying to create a basic calculator program using Java. But, the result which I get is all together. Can anyone help me with this?

1 Answer

0 votes
by (26.7k points)

You can try the below code, this will help:

import java.util.*;

import java.util.Scanner;

public class Intellipaat {

    public static void main(String[] args)

    {

        System.out.println("Enter first and second number:");

        Scanner inp= new Scanner(System.in);

        int num1,num2;

        num1 = inp.nextInt();

        num2 = inp.nextInt();

        int ans;

        System.out.println("Enter your selection: 1 for Addition, 2 for substraction 3 for Multiplication and 4 for division:");

        int choice;

        choose = inp.nextInt();

        switch (choice){

        case 1:

            System.out.println(add( num1,num2));

            break;

        case 2:

            System.out.println(sub( num1,num2));

            break;      

        case 3:

            System.out.println(mult( num1,num2));

            break;

        case 4:

            System.out.println(div( num1,num2));

            break;

            default:

                System.out.println("Illigal Operation");

        }

    }

    public static int add(int a, int b)

    {

        int result = a + b;

        return result;

    }

    public static int sub(int a, int b)

    {

        int result = a-b;

        return result;

    }

    public static int mult(int a, int b)

    {

        int result = a*b;

        return result;

    }

    public static int div(int a, int b)

    {

        int result = a/b;

        return result;

    }

}

I hope this will help.

Want to become a Java expert? Join Java Course now!!

Want to know more about Java? Watch this video on Java Tutorial | Java Course:

Related questions

0 votes
1 answer
asked Feb 19, 2021 in Java by Harsh (1.5k points)
0 votes
1 answer
0 votes
1 answer
asked Apr 3, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
asked Mar 30, 2021 in Java by sheela_singh (9.5k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...