Programming Examples

Java program to input a floating point number and round it off to the nearest integer


Write a program to input a floating point number and round it off to the nearest integer.

For Example:

INPUT: Enter a floating point number: 12.3

OUTPUT: Rounded off to nearest integer:12

INPUT: Enter a floating point number: 14.5

OUTPUT: Rounded off to two places of decimal:15

Solution

import java.util.*;
class RoundOff
{
	public static void main(String arr[])
	{
		Scanner sc=new Scanner(System.in);
		float n;
		int r;
		System.out.println( “ Enter a floating point number:”);
		n=sc.nextFloat();
		r=(int)(n+0.5f);
		System.out.println(“Answer=”+r);
	}
}
Output

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz