Write a Java program that reads in two floating-point numbers and tests whether they are the same up to three decimal places.

Write a Java program that reads in two floating-point numbers and tests whether they are the same up to three decimal places.

Test Data
Input floating-point number: 25.586
Input floating-point another number: 25.589

Code

import java.util.Scanner;

public class Threedecimalplaces {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Input floating Number");
        double x = sc.nextDouble();
        System.out.println("Enter other Input floating Number");
        double y = sc.nextDouble();
        x = Math.round(x * 1000);
        x = x / 1000;
        y = Math.round(y * 1000);
        y = y / 1000;
        if (x == y)
        {
            System.out.println("They are the same up to three 
decimal places");
        }
        else
        {
            System.out.println("They are different");
        }

    }
    
}

Output

Input floating-point number:  25.586
Input floating-point another number: 25.589
They are different




Post a Comment

0 Comments