java

Java program to print an integer

Java program to print an integer value

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?

input- output

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

Java code to print an integer

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

  • Integer variable num is declared
  • Integer variable num is initialized
  • Then, the program has displayed the output (value of variable num ) using  System.out.print()

 

Java code print an integer (Entered by user)

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

  1. The program requires input from the user
  2. Then the user enters the input value using the keyboard
  3. The program will read the input using scanner class and stores the num variable
  4. Then, the program Display the value which  is in variable num using System.out.print()

 

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

The operator in Java language

Java hello world

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.