write a program to input two numbers and print the program. using scanner class
plzz give answer quickly i'll mark you brainlist..!
Share
write a program to input two numbers and print the program. using scanner class
plzz give answer quickly i'll mark you brainlist..!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Answer:
To write a Java program that takes input for two numbers and then prints them, you can use the `Scanner` class for input. Here's a simple example:
```java
import java.util.Scanner;
public class InputAndPrintNumbers {
public static void main(String[] args) {
// Create a Scanner object to read input from the console
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the first number
System.out.print("Enter the first number: ");
// Read the first number from the user
double firstNumber = scanner.nextDouble();
// Prompt the user to enter the second number
System.out.print("Enter the second number: ");
// Read the second number from the user
double secondNumber = scanner.nextDouble();
// Close the scanner to free up resources
scanner.close();
// Print the two numbers
System.out.println("You entered the following numbers:");
System.out.println("First Number: " + firstNumber);
System.out.println("Second Number: " + secondNumber);
}
}
```
In this program:
1. We import the `Scanner` class at the beginning of the program.
2. We create a `Scanner` object called `scanner` to read input from the console.
3. We use `System.out.print` to prompt the user to enter the first number and then use `scanner.nextDouble()` to read the input as a double.
4. We repeat the same process for the second number.
5. We close the `scanner` to release the resources when we're done with it.
6. Finally, we print the two numbers back to the user.
Compile and run this program, and it will allow you to input two numbers and then display them on the console.