Aug 1, 2024

Java Program to Perform All Arithmetic Operations

In the world of programming, learning basic tasks like adding, mutiplying, deviding two numbers is a starting point for every beginner. So, in this post, we are going to explore how to perform all arithmetic operations using two numbers in Java, with a step-by-step explanation of the program. But before delving into that, you need to understand what the Arithmetic Operators in Java are.

There are total five arithmetic operators in Java as shown in below image:

Java Program To Perform All Arithmetic Operations, Basic Java Programming Examples, Java Exercies, Core java examples



In below example I am going to show you how to perform addition of two numbers using '+' arithmetic operator in java. Here is the step by step explanation of below program. 

  1. Create a Scanner class object to accept input from the user.
  2. Accept two numbers from the user using the `Scanner` class object and store the accepted values in the variables called num1 and num2.
  3. Declare a third variable called add to store the addition of those two numbers.
  4. Print the addition on the console.

Java Program to Add Two Numbers:

import java.util.Scanner;

public class AddNumbers
{
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    
	    System.out.println("Enter the first number: ");
	    int num1 = sc.nextInt();
	    System.out.println("Enter the second number: ");
	    int num2 = sc.nextInt();
	    
            int add = num1 + num2;	    
	    System.out.println("Addition  of Given two Numbers is: "+add);
	}
}

Output:

Enter the first number:
55
Enter the second number:
45
Addition of Given two Numbers is: 100



Below are examples of other arithmetic operators in Java. The logic of the program will be the same as the above program. We just need to change the operator. Like above, we have used + for addition; similarly, we can use - for subtraction, * for multiplication, / for division, and % operator to find the remainder.   


Java Program to Subtract Two Numbers:

import java.util.Scanner;

public class SubtractNumber
{
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    
	    System.out.println("Enter the first number: ");
	    int num1 = sc.nextInt();
	    System.out.println("Enter the second number: ");
	    int num2 = sc.nextInt();
	    
            int subtract = num1 - num2;	    
	    System.out.println("Subtraction  of Given two Numbers is: "+subtract);
	}
}

Output:

Enter the first number:
55
Enter the second number:
45
Subtraction of Given two Numbers is: 10



Java Program to Multiply Two Numbers:

import java.util.Scanner;

public class MultiplyNumbers
{
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    
	    System.out.println("Enter the first number: ");
	    int num1 = sc.nextInt();
	    System.out.println("Enter the second number: ");
	    int num2 = sc.nextInt();
	    
            int multiply = num1 * num2;	    
	    System.out.println("Multiplication  of Given two Numbers is: "+multiply);
	}
}

Output:

Enter the first number:
55
Enter the second number:
45
Multiplication of Given two Numbers is: 2475



Java Program to Divide Two Numbers:

import java.util.Scanner;

public class DivideNumber
{
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    
	    System.out.println("Enter the first number: ");
	    int num1 = sc.nextInt();
	    System.out.println("Enter the second number: ");
	    int num2 = sc.nextInt();
	    
            int divide = num1 / num2;	    
	    System.out.println("Division  of Given two Numbers is: "+divide);
	}
}

Output:

Enter the first number:
55
Enter the second number:
5
Division of Given two Numbers is: 11



Java Program to Print Remainder of Two Numbers:

import java.util.Scanner;

public class Modulo
{
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    
	    System.out.println("Enter the first number: ");
	    int num1 = sc.nextInt();
	    System.out.println("Enter the second number: ");
	    int num2 = sc.nextInt();
	    
            int mod = num1 % num2;	    
	    System.out.println("Remainder of Given two Numbers is: "+mod);
	}
}

Output:

Enter the first number:
10
Enter the second number:
3
Remainder of Given two Numbers is: 1

No comments:

Post a Comment

Share your thoughts or ask questions below!...👇

Featured Post

Java Interview Questions for 2 to 5 Years Experience

In this post, we are going to cover the most frequently asked Java interview questions for a 2 to 5 years experienced Java developer. It con...