Java Code for Addition of Two Numbers. In the world of programming, the addition of two numbers is a basic simple task. In this article, we will write a simple Java program to add two numbers entered by the user.
Table of Contents
Writing Java Program for Addition
The process of adding two numbers in Java involves three key steps. Taking user input by using the scanner class, performing the addition operation, and printing the result.
import java.util.Scanner;
public class AddTwoNo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number");
int n1 = sc.nextInt();
System.out.println("Enter the second number");
int n2 = sc.nextInt();
int result = n1+n2;
System.out.println("result\n"+ n1 + "+ "+n2+" = "+result);
}
}
Output
Writing a Java program to add two numbers is a simple yet powerful exercise for beginners.
Happy Coding & Learning