Addition of Two Number in Java – A Beginner’s Guide

 Java is a popular programming language used for building everything from mobile apps to enterprise software. One of the first programs most beginners write is the addition of two number in Java. It’s a simple task, but it helps in understanding how variables, input/output, and operators work.

Let’s look at how to perform this basic operation.

Java Program for Addition of Two Number


public class Addition { public static void main(String[] args) { int a = 10; int b = 20; int result = a + b; System.out.println("Addition of two number in Java: " + result); } }

Output:


Addition of two number in Java: 30

Explanation:

  • int a = 10; and int b = 20; – We declared two integer variables and assigned values.

  • int result = a + b; – We used the + operator to add the numbers.

  • System.out.println(...) – This line displays the output on the console.

Try It With User Input

To make it more dynamic, here’s how you can take user input:


import java.util.Scanner; public class AdditionWithInput { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first number: "); int num1 = input.nextInt(); System.out.print("Enter second number: "); int num2 = input.nextInt(); int sum = num1 + num2; System.out.println("Addition of two number in Java: " + sum); input.close(); } }

Conclusion

The addition of two number in Java is a fundamental concept that helps you understand how Java handles variables, input, and output. It’s a great starting point for beginners who are just stepping into programming with Java.

Keep practicing and try extending the program with subtraction, multiplication, or even GUI-based input. Happy coding!

Comments

Popular posts from this blog

Python features

How to Find GCD of Two Numbers in Java

Master AWS with the Best Training in Pune – Join Technogeeks Today!