w3resource

Java: Print a floating-point number in a specified format

Java Conditional Statement: Exercise-4 with Solution

Check Positive, Negative, or Zero (with Range)

Write a Java program that reads a floating-point number and prints "zero" if the number is zero. Otherwise, print "positive" or "negative". Add "small" if the absolute value of the number is less than 1, or "large" if it exceeds 1,000,000.

Test Data
Input value: 25

Pictorial Presentation:

Java conditional statement Exercises: Print a floating-point number in a specified format

Sample Solution:

Java Code:

import java.util.Scanner;
public class Exercise4 {

    
  public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Input value: ");
        double input = in.nextDouble();

        if (input > 0)
        {
            if (input < 1)
            {
                System.out.println("Positive small number");
            }
            else if (input > 1000000)
            {
                System.out.println("Positive large number");
            }
            else
            {
                System.out.println("Positive number");
            }
        }
        else if (input < 0)
        {
            if (Math.abs(input) < 1)
            {
                System.out.println("Negative small number");
            }
            else if (Math.abs(input) > 1000000)
            {
                System.out.println("Negative large number");
            }
            else
            {
                System.out.println("Negative number");
            }
        }
        else
        {
            System.out.println("Zero");
        }
    }
}

Sample Output:

Input value: 25                                                                                               
Positive number

Flowchart:

Flowchart: Java Conditional Statement Exercises - Print a floating-point number in a specified format

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to to find the largest of three numbers.
Next: Write a Java program that keeps a number from the user and generates an integer between 1 and 7 and displays the name of the weekday.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/conditional-statement/java-conditional-statement-exercise-4.php