sum of two numbers in java

In this article, we are going to write program in java to perform sum of two numbers.

Example:-

If n1=5, n2=5, then the output will be 10.

Steps:-

  • Take input for first number.
  • Take input for second number.
  • print first number + second number.

Java code to perform sum of two numbers.

package gangforcode;
import java.util.*;
public class SumOfTwoNumbers {

	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		System.out.println("Enter first number");
		int n1=sc.nextInt();
		System.out.println("Enter second number ");
		int n2=sc.nextInt();
		int sum=0;
		sum=n1+n2;
		System.out.println("sum of two numbers is "+sum);
	}

}

Output:-

sum of two numbers.

Leave a Comment