Java Conditional Programs
Learn Java conditional programs with if, else, and switch. Work through grades, leap years, bills, and number checks with code, explanations, and output.
20 programs · open one, then the next
Start with Grade Calculator- 01Grade CalculatorBeginnerAssign a letter grade in Java with Scanner and descending if/else thresholds. Exactly 90 earns A; the example assumes a percentage between 0 and 100.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 02Vowel or ConsonantBeginnerIdentify a vowel or consonant in Java with Character.toLowerCase and letter-range checks. English letters work in either case; non-letters are rejected.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 03Check Alphabet/Digit/Special CharacterBeginnerGroup a character in Java with a-z, A-Z, and 0-9 range checks after Scanner input. Only English letters match; all other non-digits reach the final branch.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 04Check Leap YearBeginnerTest a leap year in Java by checking remainders for 400, 100, and 4 in order. Scanner reads the year; 1900 fails while 2000 passes the Gregorian rules.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 05Check Armstrong NumberIntermediateCheck an Armstrong number in Java by counting digits and adding their powers with nested loops. 153 passes; large int inputs can overflow the sum.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 06Check Perfect NumberIntermediateRecognise a perfect number in Java by adding divisors from 1 through n / 2 using %. For 28 the sum is 28; zero is explicitly excluded from the result.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 07Check Strong NumberIntermediateTest a strong number in Java with a factorial helper and a do/while digit loop. Compare the sum with the input; 145 passes, while zero is not strong.Open example →javaimport java.util.Scanner; public class Main { private static int factorial(int n) {
- 08Check Palindrome NumberBeginnerCheck a numeric palindrome in Java by extracting digits with % 10 and building a long reverse. Negative inputs fail, and numeric input drops leading zeros.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 09Max of 3 Using Nested IfBeginnerSelect the greatest of three integers in Java using nested if statements and Scanner input. Each branch assigns the maximum; >= handles equal values.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 10Electricity Bill CalculatorIntermediateCalculate a block-rate electricity bill in Java with if/else and Scanner. Charge only extra units at higher rates; these practice rates exclude taxes.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 11Menu-Driven CalculatorBeginnerRun a four-option calculator in Java with Scanner and switch cases. Each case ends with break; division by zero and invalid menu choices get messages.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 12Profit or LossBeginnerCompare cost and selling prices in Java with Scanner and if/else. Subtraction gives a positive profit or loss amount; equal prices report neither.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 13Triangle Validity CheckBeginnerTest triangle side lengths in Java with three pair-sum comparisons joined by &&. All must pass strictly; 1, 2, 3 fails because equality gives a flat line.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 14Roots of Quadratic EquationIntermediateSolve a quadratic in Java using the discriminant and Math.sqrt for real or complex roots. Reject a zero coefficient; decimal comparisons can be approximate.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 15Person Eligibility (Age Check)BeginnerApply an age threshold in Java using Scanner.nextInt and >= 18. Exactly 18 passes this classroom voting check; other eligibility rules are not tested.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 16Library Fine CalculationIntermediateWork out a library fine in Java by adding daily-rate blocks with if/else. Eight late days cost 11 under these sample rules; on-time returns cost zero.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 17Attendance Percentage CalculatorBeginnerCalculate attendance in Java with attended * 100.0 / held and a 75% check. Reject invalid class counts before division; exactly 75% passes the sample rule.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 18Temperature ClassificationBeginnerLabel a Celsius reading in Java as Cold, Moderate, or Hot using Scanner and if/else. The sample bands include both 10 and 30 in the Moderate range.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 19Discount CalculatorBeginnerCalculate a discount in Java by testing spending thresholds from highest to lowest. Subtract the chosen percentage from the full amount to get net pay.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 20Basic Authentication ProgramBeginnerCompare demo login text in Java using Scanner.nextLine, equals, and &&. Both values must match; hardcoded credentials make this unsuitable for real users.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {