The Arithmetic operators are used to perform arithmetic operations. The Arithmetic operators are binary operators that work with integers, floating point numbers and even characters (i.e. they can be used with any primitive type except the boolean).
There are five arithmetic operators available in Java which include addition (+), subtraction (-), multiplication (*), division (/) and modulus (%) operator. The modulus operator tells us what would be the remainder after integer division is performed on a particular expression. Unlike C/C++, the modulus operator can also be applied to floating point values. The rest of the operators have same meaning as they have in mathematics.
The operands acted on by arithmetic operators must represent numeric values. Thus the operands can be integer quantities, floating point quantities or characters. The Arithmetic operators when used with characters operate on Unicode values of the characters.
The result of division of one integer quantity by another is referred to as integer division as this operation results in a quotient without a decimal point. On the other hand, if division is carried out with two floating point numbers or with one floating point and one integer number, the result will be a floating point quotient.
//Program Showing Arithmetic Operations
public class ArithmeticOperators
{
public static void main (String[] args)
{
float x=10.5f ,y=2.3f ;
System.out.println(" x+y = " + (x+y));
System.out.println(" x-y = " + (x-y));
System.out.println(" x*y = " + (x*y));
System.out.println(" x/y = " + (x/y));
System.out.println(" x%y = " + (x%y));
}
}
Java Example to Perform Arithmetic Operation Using Scanner Class.
import java.util. *;
class ArithmeticOperatorsUsingScanner
{
public static void main(String args[])
{
int a,b;
Scanner scan=new Scanner(System.in);
System.out.print("Enter the Value of a : ");
a=scan.nextInt();
System.out.print("Enter the Value of b : ");
b=scan.nextInt();
System.out.println("Addition is : " +(a+b));
System.out.println("Subtraction is : "+( a-b));
System.out.println("Multiplication is : "+( a*b));
System.out.println("Division is : "+( a/b));
System.out.println("Modulus is : "+( a%b));
}
}
Dinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.
Related Articles
Basic Courses
Advance Courses