Addition of two numbers in java




Certainly! Here's a simple Java program that adds two numbers and displays the result:

import java.util.Scanner; public class AddTwoNumbers { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the first number: "); double firstNumber = input.nextDouble(); System.out.print("Enter the second number: "); double secondNumber = input.nextDouble(); double sum = firstNumber + secondNumber; System.out.println("The sum of " + firstNumber + " and " + secondNumber + " is: " + sum); } }



In this program:

  • We import the java.util.Scanner class to take input from the user.
  • We create a Scanner object called input to read user input.
  • We prompt the user to enter two numbers, and we use input.nextDouble() to read and store these numbers as firstNumber and secondNumber.
  • We calculate the sum of the two numbers and store it in the variable sum.
  • Finally, we print the result, showing the sum of the two numbers.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.