Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Wednesday, February 5, 2025

User input for addition of two numbers JAVA

Java program that takes user input for two numbers and performs addition: 


import java.util.Scanner;  // Import the Scanner class


public class AdditionProgram 

{

    public static void main(String[] args) {

        // Create a Scanner object for user input

        Scanner scanner = new Scanner(System.in);

        

        // Prompt user for first number

        System.out.print("Enter the first number: ");

        int num1 = scanner.nextInt();

        

        // Prompt user for second number

        System.out.print("Enter the second number: ");

        int num2 = scanner.nextInt();

   

        // Perform addition

        int sum = num1 + num2;

        

        // Display result

        System.out.println("The sum is: " + sum);

        

        // Close scanner

        scanner.close();

    }

}




Output :





No comments:

Post a Comment