Table of Contents
ToggleIntroduction
Addition is a fundamental operation in Java that allows us to combine two numbers and obtain their sum. Learning to add numbers is important for beginners and advanced Java programmers.
In this blog, we will look into various techniques to find the sum, including using arithmetic operator and compound assignment operator, as well as the “Integer.sum()” method and using a scanner. By exploring these techniques , you will gain a solid understanding of how to perform addition of two numbers in java.
Level up your skills in Java Programming. Enroll now and become an expert.
Addition of two numbers in java Using Arithmetic Operator
One common and effective method for adding two numbers in Java is by using the standard arithmetic addition operation.
Algorithm Of Addition of two numbers in java:
Step 1: Defining Variables
Before adding two numbers, we need to declare and initialize variables to store those numbers.
Declaring variables in Java is as follows:
We declared two variables, “number1” & “number2”, and assigned them 5 and 7, respectively.
Step 2: Adding the Numbers
We’ll create a new variable named “sum” and assign the sum of number1 and number2 to it to add the numbers. In Java, you can perform addition using the “+” operator.
Here’s how you can add the numbers:
In the code sample above, we added “number1” and “number2” and stored the result in sum.
“The int data type specifies that the sum will hold an integer value.”
Step 3: Displaying the Result
In Java, you can use the “System.out.println()” statement to output the value of a variable or any other text.
Here’s how you can display the sum:
In the code above, we concatenate the string “The sum is: “ with the value stored in the sum variable using the “+” operator. The result will be printed on the console, providing the sum of the two numbers.
Complete Example Code:
Here’s the complete example code for adding two numbers in Java:
Also Read : Tokens In Java
Addition of two numbers in java Using “+=” Compound Assignment Operator
Another method is to utilize the compound assignment operator “+=”, which combines addition and assignment in a single step. This operator adds the right-side value to the left-side variable & assigns the result back to it.
Algorithm Of Addition of two numbers in java Using “+=” Compound Assignment Operator
Step 1: Declare and initialize the variables
- Define two variables to hold the numbers you want to add.
- Assign appropriate values to the variables.
Step 2: Execute Addition operation
- Use the “+=” compound assignment operator to add the second number to the first number.
- This operation updates the value of the first number by adding the second number to it.
Step 3: Display the result
- The total of the two numbers is the first number’s updated value.
For Example:
- The code modifies the value of “number1” directly instead of creating a new variable for the sum.
- It uses the “+=” compound assignment operator to add “number2” to “number1”.
- This operation updates the value of “number1” by adding “number2” to it.
- The updated value of “number1” represents the sum of the two numbers.
- Finally, the sum is displayed on the console using “System.out.println()”.
Addition of two numbers in java Using the “Integer.sum()” Method:
Java provides a built-in method called “sum()” in the “Integer” class, which allows you to add two numbers and obtain their sum. This method takes two integer arguments and returns their sum.
Algorithm Of Addition of two numbers in java Using “Integer.sum()” Method:
Step 1: Declare and initialize the variables
- Define two variables to hold the numbers you want to add.
- Assign appropriate values to the variables.
Step 2: Perform the addition operation
- Use the “Integer.sum()’ method to add the two numbers together.
- Pass the two numbers as arguments to the “Integer.sum()” method.
Step 3: Store the result
- Assign the returned value from the “Integer.sum()” method to a variable to store the sum.
Step 4: Display the result
For Example:
- We declared two variables, “number1” & “number2”, and assigned them 5 and 7, respectively.
- We use the “Integer.sum()” method to add “number1” and “number2” together.
- The returned value from the method is stored in the sum variable.
- We print the sum on the console using “System.out.println()”.
Addition of two numbers in java Using Scanner:
If you want to add numbers entered by a user at runtime, you can use the Scanner class to read input from the console.
Algorithm Of Addition of two numbers in java Using Scanner
Step 1: Import the necessary class
- Import the java.util.Scanner class at the beginning of your code.
Step 2: Create a Scanner object
- Create a Scanner object to read user input.
Step 3: Prompt the user for input
- Show a message asking for the first number.
- Use the “Scanner” object to read and store the first number.
- Show a message asking for the second number.
- Use the “Scanner” object to read and store the second number.
Step 4: Execute Addition operation
- Add the two numbers and store the result in a variable.
Step 5: Display the result
- Print the value of the variable that holds the sum on the console.
For Example:
- In the given code, we first import the “java.util.Scanner” class to use the “Scanner” object for user input.
- We create a “Scanner” object named scanner.
- We prompt the user to enter the first and second numbers using “System.out.print()” statements, and use “scanner.nextInt()” to read and store the input values.
- Next, we perform the addition operation by adding “number1” and “number2” together, and store the result in the sum variable.
- We print the sum on the console using “System.out.println()”.
How To Add Floating-Point Numbers In Java
Use “float” or “double” data types to add floating-point numbers (numbers with decimal places) instead of integers.
The addition process remains the same; you just need to declare the variables accordingly.
Algorithm Of Addition of Floating Point numbers in java
Step 1: Declare and initialize the variables
- Choose appropriate data types (float or double) to hold the floating-point numbers.
- Declare and assign values to the variables.
Step 2: Perform the addition operation
- Use the + operator to add the floating-point numbers together.
- Assign the result to a variable.
Step 3: Display the result
- Print the value of the variable that holds the sum on the console.
For Example:
- We declare two variables, “number1” & “number2”, of type double and assign them the desired floating-point values.
- We then use the “+” operator to add the two numbers together and store the result in the sum variable.
- We print the sum on the console using “System.out.println()”.
Conclusion
In conclusion, as you continue to explore and experiment with the addition of two numbers in Java, you will build upon your knowledge and skills. Throughout this blog, we explored different techniques and methods to perform addition in Java. By understanding these concepts, you can confidently write code to add numbers and obtain accurate results.
Checkout our courses on the Java programing below
👉https://technogeekscs.com/courses/core-java-training 👈
👉 https://technogeekscs.com/courses/advance-java-course 👈