Software Development Exam  >  Software Development Notes  >  Basics of Java  >  Java Arithmetic Operators

Java Arithmetic Operators | Basics of Java - Software Development PDF Download

Introduction

Arithmetic operators are an essential part of any programming language, including Java. These operators allow you to perform various mathematical calculations such as addition, subtraction, multiplication, division, and more. In this article, we will explore the different arithmetic operators available in Java, along with examples and code explanations to help you understand their usage.

Arithmetic Operators in Java

Java provides several arithmetic operators that you can use to perform mathematical operations. Here are the commonly used arithmetic operators in Java:

Addition Operator (+)

The addition operator (+) is used to add two values together. It can be used with numeric types such as integers (int), floating-point numbers (float and double), and characters (char). Here's an example:

int a = 5;

int b = 3;

int result = a + b;

System.out.println("Result: " + result);

Output

Result: 8

In the above code, we declare two variables a and b and assign them values. Then, we use the addition operator to add a and b together and store the result in the result variable. Finally, we print the result.

Subtraction Operator (-)

The subtraction operator (-) is used to subtract one value from another. It works similarly to the addition operator but performs subtraction instead. Let's see an example:

int a = 10;

int b = 7;

int result = a - b;

System.out.println("Result: " + result);

Output

Result: 3

In the above code, we subtract the value of b from a and store the result in the result variable.

Multiplication Operator (*)

The multiplication operator (*) is used to multiply two values together. It takes two operands and returns their product. Here's an example:

int a = 4;

int b = 6;

int result = a * b;

System.out.println("Result: " + result);

Output

Result: 24

In the above code, we multiply the values of a and b and store the result in the result variable.

Division Operator (/)

The division operator (/) is used to divide one value by another. It performs division and returns the quotient as a result. Let's see an example:

int a = 10;

int b = 3;

int result = a / b;

System.out.println("Result: " + result);

Output

Result: 3

In the above code, we divide the value of a by b and store the result in the result variable.

Modulus Operator (%)

The modulus operator (%) is used to calculate the remainder of a division operation. It divides one value by another and returns the remainder. Here's an example:

int a = 10;

int b = 3;

int result = a % b;

System.out.println("Result: " + result);

Output

Result: 1

In the above code, we calculate the remainder when a is divided by b and store the result in the result variable.

Increment Operator (++)

The increment operator (++) is used to increase the value of a variable by 1. It can be used both as a prefix or postfix operator. Here's an example:

int a = 5;

a++; // Increment a by 1

System.out.println("Result: " + a);

Output

Result: 6

In the above code, we increment the value of a by 1 using the increment operator.

Decrement Operator (--)

The decrement operator (--) is used to decrease the value of a variable by 1. It works similarly to the increment operator. Let's see an example:

int a = 5;

a--; // Decrement a by 1

System.out.println("Result: " + a);

Output

Result: 4

In the above code, we decrement the value of a by 1 using the decrement operator.

Sample Problems

Here are a few sample problems for you to practice using arithmetic operators:

  • Calculate the area of a rectangle with a width of 5 units and a height of 8 units.
  • Find the average of three numbers: 12, 18, and 25.
  • Given the radius of a circle as 7 units, calculate its circumference.
  • A car travels at a speed of 60 km/h. Calculate the distance it covers in 2.5 hours.

Solutions:

1. Area of a rectangle = width * height

  • int width = 5;
  • int height = 8;
  • int area = width * height;
  • System.out.println("Area of the rectangle: " + area);

Output 

Area of the rectangle: 40

2. Average of three numbers = (num1 + num2 + num3) / 3

int num1 = 12;

int num2 = 18;

int num3 = 25;

double average = (num1 + num2 + num3) / 3.0;

System.out.println("Average: " + average);

Output 

Average: 18.333333333333332

3. Circumference of a circle = 2 * π * radius

double radius = 7;

double circumference = 2 * Math.PI * radius;

System.out.println("Circumference of the circle: " + circumference);

Output

Circumference of the circle: 43.982297150257104

4. Distance = speed * time

int speed = 60;

double time = 2.5;

double distance = speed * time;

System.out.println("Distance covered: " + distance + " km");

Output

Distance covered: 150.0 km

Conclusion

Arithmetic operators in Java allow you to perform various mathematical calculations. By understanding and utilizing these operators effectively, you can solve a wide range of mathematical problems in your Java programs. Remember to practice using the operators and experiment with different scenarios to strengthen your understanding. 

The document Java Arithmetic Operators | Basics of Java - Software Development is a part of the Software Development Course Basics of Java.
All you need of Software Development at this link: Software Development
60 videos|37 docs|12 tests

Up next

60 videos|37 docs|12 tests
Download as PDF

Up next

Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Java Arithmetic Operators | Basics of Java - Software Development

,

Extra Questions

,

Viva Questions

,

Exam

,

Semester Notes

,

Important questions

,

video lectures

,

Free

,

Java Arithmetic Operators | Basics of Java - Software Development

,

Sample Paper

,

Previous Year Questions with Solutions

,

study material

,

practice quizzes

,

Java Arithmetic Operators | Basics of Java - Software Development

,

past year papers

,

MCQs

,

Objective type Questions

,

Summary

,

pdf

,

shortcuts and tricks

,

ppt

,

mock tests for examination

;