Addition of Two Numbers – Java

Write a program to add two numbers in java

- Advertisement -

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);
    }
}

 

- Advertisement -

output:

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

 

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More