Java program to add two Integers
In this program, two integers 11 and 12 are stored in integer variables first and second respectively.
Then, first and second are added using the + operator, and its result is stored in another variable sum.
Finally, sum is printed on the screen using println() function.
class Main {
public static void main(String[] args) {
System.out.println("Enter two numbers");
int first = 11;
int second = 12;
System.out.println(first + " " + second);
int sum = first + second;
System.out.println("The sum is: " + sum);
}
}
Enter two numbers
11 12
The sum is: 23
Write a Java program to print an Integer Here
How to print an integer entered by an user
In this program, an object of scanner class, reader is created to take input from standard input, Which is keyboard.
Then, Enter a number prompt is printed to give the user a visual cue as to what they should do next.
reader.nextInt() the reads all entered integers from the keyword unless it encounters a new line character \n Enter. The entered integer are then saved to the integer variable number.
If you enter any character which is not an integer, the compiler will throw an InputMismatchException.
Finally, number is printed onto the standard output (system.out) - computer screen using the function println().
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = reader.nextInt();
System.out.println("You entered: " + number);
}
}
Enter a number: 10
You entered: 10
Write a Java program to add two integers
Here