Computer >> Computer tutorials >  >> Programming >> Java

Java Program to Print an Integer


In this article, we will understand how to print an integer in Java. It uses the int data type. The int data type is a 32-bit signed two's complement integer. The Minimum value is 2,147,483,648 (-2^31) and the Maximum 2,147,483,647(inclusive) (2^31 -1). Integer is generally used as the default data type for integral values unless there is a concern about memory. The default value is 0.

Input

Suppose our input is

Enter an integer: 45

Output

The desired output would be

The integer is: 45

Algorithm

Step 1- START
Step 2- Prompt the user to enter an integer value/ define the integer value in a variable
Step 3- Read the value
Step 4- Display it on the console
Step 5- STOP

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool Java Program to Print an Integer.

import java.util.Scanner;
public class PrintInteger {
   public static void main(String[] args) {
      Scanner reader = new Scanner(System.in);
      System.out.println("Required packages have been imported");
      System.out.print("A reader object has been defined ");
      System.out.print("Enter an integer: ");
      int number = reader.nextInt();
      System.out.print("The nextInt method is used to read the integer value ");
      System.out.println("The integer is: " + number);
}
}

Output

Required packages have been imported
A reader object has been defined
Enter an integer: 45
The nextInt method is used to read the integer value
The integer is: 45

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class PrintInteger {
public static void main(String[] args) {
int number;
number = 45;
System.out.print("The value for integer has been defined ");
System.out.println("The integer value is: " + number);
}
}

Output

The value for integer has been defined The integer is: 45