Table of Contents
In this tutorial, we discuss the simple concept of Write a Java program to print an integer
problem – How to write a program to print an integer in Java?
In this article, we are going to learn How to write a program to print an integer in Java language
The following shows two different ways to How to write a program to print an integer and there is an example given to each
The first Example is done by declared and initialized variable
the second Example is done through the user entered the value of the variable
Program 1
//program to print an integer in Java
class PrintInteger1{//imported java.util.Scanner package
public static void main(String args[]){
int num; //variable declaration
num=56; //initialization
System.out.print("You declared : "+num);
}
} When the above code is executed it produces the following output
you declared: 56
In this program
Program 2
//Program to print an integer in Java
import java.util.Scanner; //imported java.util.Scanner package
class PrintInteger{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner instance for receive input
//from the user - input from keyboard
System.out.print("Enter the integer to print: ");
int num=scan.nextInt();
//reads the integer value from the user
//input from keyword
System.out.print("You entered : "+num);
}
} When the above code is executed it produces the following output
Enter the integer to print:34 you entered: 34
Method
Similar post
C++ program to print an integer value
C program to print an integer value
Python program to print an integer value
Suggested for you
Data type and Variable in Java
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.