Table of Contents
Introduction
Hello Everyone, So in this article I am going to teach you how you can perform addition of two variables using java and it’s different methods such as by using hard coded values, by taking user input and with the help of user defined function etc. We will first start from the algorithm of every method then we will jump into the code and on its working and I will try my level best to make you guys to understand the very basics of this program. So that you actually know what is going on in behind the scene. Let’s get started.
Addition using hard coded values
Algorithm
- First Define a Class called Main. You can give whatever name you want to just make sure it follows proper syntax and their is no reserved keywords are used while defining any class, variable, function e.t.c.
- After defining a Class now inside the main function define a variable of int type and assign a value of 10 or whatever integer value you want.
- Define a variable named b and assign a value of 20 or any other value of type integer.
- Now define a variable called c and in c variable store the result of a + b which is 30.
- Now we are done with the addition part but we also need to show our result to the console to cross verify if we are getting correct result or not. So we will use System.out.println method to print the result to the console screen.
Code
//This is an class Called Main
public class Main
{
/* main funtion is the entry funtion for all the function we write in java class
if we want to run any of user defined funtion then we have to call it inside main funtion*/
public static void main(String[] args) {
int a = 10; // We are storing value 10 in an int type variable named a.
int b = 20; // We are storing value 10 in an int type variable named b.
int c = a + b; // We are adding a + b which is 10 + 20 and storing the result of this in c variable.
System.out.println(c); // Here we will get the result 30 on console screen
} // Here the main function ends.
} // Here the class called Main ends.
Addition by taking user input
Algorithm
- First import java scanner library which will allow us to take user input from the keyboard and then Define a Class called Main. You can give whatever name you want to just make sure it follows proper syntax and there is no reserved keywords are used while defining any class, variable, function e.t.c.
- After defining a Class now inside the main function define variables named x, y, sum. In x and y variables we are going to assign and store the user input. Then in sum variable we are going to store the result of x + y.
- Now create an object of class Scanner which is provided by java. It allows us to listen to users input and assign the value of user input to an variable.
- Now show an message which tells the user to enter the value of x.
- In next step listen to the user input by calling nextInt function with the help of Scanner object.
- Repeat the step number 4 and 5. But this time repeat it for y variable.
- After storing the value of x and y. Now store the addition result of x and y in variable called sum.
- In the final step display the result of addition to the user.
Code
import java.util.Scanner; // Import the Scanner class
class Main { // Starting of Class named Main
public static void main(String[] args) { // Starting point of the programm
int x, y, sum;
Scanner scannerObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter the value of x ");
x = scannerObj.nextInt(); // Read the user input for x
System.out.println("Enter the value of y ");
y = scannerObj.nextInt(); // Read the user input for y
sum = x + y; // Calculate the sum of x + y
System.out.println("Total of x + y is = " + sum); // Print the sum of x + y
}
}
Conclusion
So that’s how you can perform Addition of two variable using java. These two methods are the very basic and beginner ways of performing Addition of two variable using java. There are many other ways by which you can perform addition. But if you are beginner then learn these first then move to those methods. I hope you guys understood the how performing addition of two variables can be done. But still if you have any question suggestions the comment section is open for you. Please share your thoughts so we can improve the quality of articles. I will see you guys in the next article til then keep learning.
- 2 Easy method : How to call api in java
- All 4 Control Statements in Java | If-else | Switch Case
- Learn Accessibility service in android
- Learn Recycle view Android – Thecseguide
- What are Methods in java ? Types of methods