Java Two-Number Addition program

 

Java Two-Number Addition.

MOHIT TYAGI
Two number addition program in java
java
import java.util.Scanner; public class AdditionProgram { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the first number: "); int num1 = input.nextInt(); System.out.print("Enter the second number: "); int num2 = input.nextInt(); int sum = num1 + num2; System.out.println("The sum of " + num1 + " and " + num2 + " is " + sum); } }

Explanation:

  1. We first import the Scanner class from the java.util package which allows us to read input from the user.
  2. Inside the main method, we create an instance of the Scanner class called "input".
  3. We then prompt the user to enter the first number by displaying the message "Enter the first number: " and read the input using input.nextInt(), which reads an integer value.
  4. Similarly, we prompt the user to enter the second number and read the input using input.nextInt().
  5. We calculate the sum of the two numbers and store it in the variable "sum".
  6. Finally, we display the sum using System.out.println(). The output message uses string concatenation to display the original input values and the calculated sum.

Comments

Popular posts from this blog

Cryptography API

Java Applet Overview

Vector in Java