Java Conditional Statement Exercises
Java Conditional Statement Exercises
Java determines the greatest of three numbers by comparing them using nested conditional statements. It first compares the first two numbers, then the result with the third number, ultimately printing the greatest of the three .
A Java program identifies positive and negative numbers using conditional statements by checking if the input number is greater than, less than, or equal to zero. It prints 'positive' if the number is greater than zero, 'negative' if it is less than zero, and 'zero' if it equals zero .
Java determines the number of days in a month using conditional statements to check for month input and special rules for February. It considers leap years by checking divisibility conditions on the year (using 4, 100, and 400) to decide February's days .
Java determines a leap year if the year is divisible by 4, but not by 100 unless also divisible by 400. Conditional statements implement these checks to conclude if the year is a leap year .
A Java program maps integers (1 to 7) to weekday names using conditional statements or switch cases, where each integer corresponds to a specific weekday, starting from Sunday to Saturday .
Java compares two floating-point numbers for up to three decimal places by truncating or rounding the numbers to three decimal places and checking if they are equal using conditional statements .
Java uses a simple loop structure to iterate from one to ten, printing each number in the sequence of the first ten natural numbers .
To solve quadratic equations using conditional statements in Java, the program first calculates the discriminant (b² - 4ac). It uses 'if', 'else if', and 'else' statements to check the value of this discriminant. If the discriminant is positive, the program computes two real roots. If zero, it computes one real root. If negative, it indicates no real roots exist .
A Java program validates user input to correctly determine if an input is a vowel or consonant to avoid errors when non-alphabet or multicharacter strings are entered. Input is checked for character bounds and length to ensure it meets criteria .
Java uses conditional statements to evaluate a floating-point number. It checks if the number equals zero, is positive, or is negative. For non-zero numbers, it adds 'small' if the absolute value is less than 1, or 'large' for values exceeding 1,000,000 .