Addition of Two Numbers – Java

Write a program to add two numbers in java

Here we will see two programs to add two numbers, we will take numbers from the user and we will add them.

import package Rootuser;

import java.util.Scanner;
public class AddTwoNumbers2 {

    public static void main(String[] args) {
        
        int num1, num2, sum;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter First Number: ");
        num1 = sc.nextInt();
        
        System.out.println("Enter Second Number: ");
        num2 = sc.nextInt();
        
        sc.close();
        sum = num1 + num2;
        System.out.println("Sum of these numbers: "+sum);
    }
}

 

output:

Enter First Number: 
20
Enter Second Number: 
20
Sum of these numbers: 40

 

Comments (0)
Add Comment