Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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

Java Conditional Statement: Exercise-4 with Solution

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.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe