Write a Java program that accepts a float value as input. Your task is to divide this float value by 2. The program should then print the output as an integer value after rounding the result down. For example, if the input is 5.3, the program should print 2, not 3.
Type casting
Rounding down a halved float value to an integer
Report a typo
Sample Input 1:
5.3Sample Output 1:
2Sample Input 2:
3.8Sample Output 2:
1Write a program in Java 17
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Create Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Read float value from user and assign it to floatVariable
float floatVariable = scanner.nextFloat();
// Divide the float variable by 2 and round down the result
// Here you need to write the code to perform the division and the type cast operation
// The result should be of integer type
// Print the integer result
// Here again, you need to write the code part
// Close the Scanner
scanner.close();
}
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.