Java program to add two numbers

Basic Java programming exercises

Java Tutorial Java Exercises Data Structures

Write a Java program to input two numbers from user and calculate their sum. Java program to add two numbers and display their sum as output. How to add two numbers in Java programming.

Required knowledge

Arithmetic operators, Data types, Basic Input/Output

Program to add two numbers
/** * Java program to find sum of two numbers */ import java.util.Scanner; class Test { public static void main(String args[]) { int num1, num2, sum; Scanner op=new Scanner(; /* * Read two numbers from user */ System.out.print("Enter first number: "); num1=op.nextInt(); System.out.print("Enter second number:"); num2=op.nextInt(); /* Adding both number is simple and fundamental */ sum = num1 + num2; /* Prints the sum of two numbers */ System.out.println("Sum of "+num1+" and "+num2+" = "+sum); } }


Enter first number: 31 Enter second number:36 Sum of 31 and 36 = 67