Arithmetic operators in java with example

Different Arithmetic operators used java are shown in table.

Operator function
* Multiplication
/ Division
% Remainder
+ Addition
- Subtraction

* operator is used give product of 2 numbers.

/ Operator gives Quotient after dividing Divisor and Dividend. Divisor should be nonzero number.

% operator gives remainder after division of 2 numbers. % operator can be used only for integer data type.

+ operator gives sum of 2 numbers.

- operator used to subtract one number from another number.

Note:-

# If both number are positive than answer will be in positive number.
# If either or both number are negative than answer will be either in positive or in negative, it is machine dependent.

Example:-

Program to find sum of 2 number.

import java.util.Scanner;

class find_sum{
public static void main(String arg[]){
int num1, num2, sum;
Scanner data = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = data.nextInt();
System.out.println("Enter 2nd number");
num2 = data.nextInt();
sum=num1+num2;
System.out.println("sum of 2 number:"+sum);
}
}

Program to find product of 2 number.

import java.util.Scanner;

class find_sum{
public static void main(String arg[]){
int num1, num2, sum;
Scanner data = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = data.nextInt();
System.out.println("Enter 2nd number");
num2 = data.nextInt();
sum=num1*num2;
System.out.println("Product of 2 number:"+sum);
}
}

Program to find subtract 2 number.

import java.util.Scanner;

class find_sum{
public static void main(String arg[]){
int num1, num2, sum;
Scanner data = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = data.nextInt();
System.out.println("Enter 2nd number");
num2 = data.nextInt();
sum=num1-num2;
System.out.println("answer after subtract 2 number:"+sum);
}
}

Program to find division 2 number.

import java.util.Scanner;

class group{
public static void main(String arg[]){
int num1, num2, sum;
Scanner data = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = data.nextInt();
System.out.println("Enter 2nd number");
num2 = data.nextInt();
if(num2!=0)
{
sum=num1/num2;
System.out.println("Quotient:"+sum);
}
else
System.out.println("Divisor should be nonzero number");
}
}

Program to find remainder.
import java.util.Scanner;

class group{
public static void main(String arg[]){
float num1, num2;
double sum;
Scanner data = new Scanner(System.in);
System.out.println("Enter 1st number");
num1 = data.nextInt();
System.out.println("Enter 2nd number");
num2 = data.nextInt();
if(num2!=0)
{
sum=num1%num2;
System.out.println("Remainder:"+sum);
}
else
System.out.println("Divisor should be nonzero number");
}
}


Previous Code:-
User Input

List Code:-
Java codes

Next Code:-
Logical operators

Java Programs:-
List of Java Programs

Leave reply

Add your comments here

Back to Top