Java programs with user input
Answers
Answered by
0
Input by user in java:
Output:
Input first number: 22
Input Second number: 28
Sum of given a number: 50
Explanation:
Program to user input values:
Program:
import java.util.*; //import package
public class Main //defining class
{
public static void main(String[] args)//defining main
{
int ax,bx,sum=0; //defining integer variable
Scanner ob2=new Scanner(System.in); //creating scanner class object
System.out.print("Input first number: "); //message
ax=ob2.nextInt(); //input first number
System.out.print("Input Second number: "); //message
bx=ob2.nextInt(); // input second number
sum=ax+bx; //adding number
System.out.print("Sum of given a number: "+sum);//print value
}
}
Description of the above program as follows:
- In the above java code, first import the package, then defining the class that is "Main", inside the class the main method is defined, in this method three integer variable "ax, bx, and sum" is defined,
- The "ax and bx" variable use scanner class concept for user input, after input both variable the sum variable, is used.
- In the sum variable, we add both variables and uses the print function to print the sum variable value.
Learn more:
- User input in java: https://brainly.in/question/13195735?
Similar questions