WhatsApp Group Join Now
Telegram Group Join Now
Instagram Group Join Now

Java Series #2: If else Questions

If-Else Programming Challenges

1. Even or Odd Easy

Problem

Write a program to check if a number is even or odd.

Input Format
  • The first line contains a single integer n.
Output Format

Print Even if the number is even, otherwise print Odd.

Sample Input 1
4
Sample Output 1
Even
Code
import java.util.Scanner;

class EvenOrOdd {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n % 2 == 0) {
            System.out.println("Even");
        } else {
            System.out.println("Odd");
        }
    }
}

2. Multiple of 5 Easy

Problem

Write a program to check if a number is a multiple of 5.

Input Format
  • The first line contains a single integer n.
Output Format

Print Yes if the number is a multiple of 5, otherwise print No.

Sample Input 1
15
Sample Output 1
Yes
Code
import java.util.Scanner;

class MultipleOfFive {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n % 5 == 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

3. Multiple of 10 Easy

Problem

Write a program to check if a number is a multiple of 10.

Input Format
  • The first line contains a single integer n.
Output Format

Print Yes if the number is a multiple of 10, otherwise print No.

Sample Input 1
100
Sample Output 1
Yes
Code
import java.util.Scanner;

class MultipleOfTen {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n % 10 == 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

4. Multiple of 3 and 5 Medium

Problem

Write a program to check if a number is a multiple of both 3 and 5.

Input Format
  • The first line contains a single integer n.
Output Format

Print Yes if the number is a multiple of both 3 and 5, otherwise print No.

Sample Input 1
15
Sample Output 1
Yes
Code
import java.util.Scanner;

class MultipleOfThreeAndFive {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n % 3 == 0 && n % 5 == 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

5. Multiple of 5, 3, and 7 Hard

Problem

Write a program to check if a number is a multiple of 5, 3, and 7.

Input Format
  • The first line contains a single integer n.
Output Format

Print Yes if the number is a multiple of 5, 3, and 7, otherwise print No.

Sample Input 1
105
Sample Output 1
Yes
Code
import java.util.Scanner;

class MultipleOfFiveThreeSeven {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n % 5 == 0 && n % 3 == 0 && n % 7 == 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

6. Two Digit Number Easy

Problem

Write a program to check if a number is a two-digit number.

Input Format
  • The first line contains a single integer n.
Output Format

Print Yes if the number is a two-digit number, otherwise print No.

Sample Input 1
45
Sample Output 1
Yes
Code
import java.util.Scanner;

class TwoDigitNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n >= 10 && n <= 99) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

7. Three Digit Number Easy

Problem

Write a program to check if a number is a three-digit number.

Input Format
  • The first line contains a single integer n.
Output Format

Print Yes if the number is a three-digit number, otherwise print No.

Sample Input 1
123
Sample Output 1
Yes
Code
import java.util.Scanner;

class ThreeDigitNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n >= 100 && n <= 999) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

8. Three Digit Number and Multiple of 10 Medium

Problem

Write a program to check if a number is a three-digit number and a multiple of 10.

Input Format
  • The first line contains a single integer n.
Output Format

Print Yes if the number is a three-digit number and a multiple of 10, otherwise print No.

Sample Input 1
120
Sample Output 1
Yes
Code
import java.util.Scanner;

class ThreeDigitMultipleOfTen {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n >= 100 && n <= 999 && n % 10 == 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

9. Three Digit Number and Multiple of 3, 5, and 10 Hard

Problem

Write a program to check if a number is a three-digit number and a multiple of 3, 5, and 10.

Input Format
  • The first line contains a single integer n.
Output Format

Print Yes if the number is a three-digit number and a multiple of 3, 5, and 10, otherwise print No.

Sample Input 1
150
Sample Output 1
Yes
Code
import java.util.Scanner;

class ThreeDigitMultipleOfThreeFiveTen {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n >= 100 && n <= 999 && n % 3 == 0 && n % 5 == 0 && n % 10 == 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

10. Number Ends with Zero Easy

Problem

Write a program to check if a number ends with zero.

Input Format
  • The first line contains a single integer n.
Output Format

Print Yes if the number ends with zero, otherwise print No.

Sample Input 1
30
Sample Output 1
Yes
Code
import java.util.Scanner;

class NumberEndsWithZero {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n % 10 == 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

11. ASCII Value of Character Easy

Problem

Write a program to print the ASCII value of a given character.

Input Format
  • The first line contains a single character ch.
Output Format

Print the ASCII value of the given character.

Sample Input 1
A
Sample Output 1
65
Code
import java.util.Scanner;

class AsciiValueOfCharacter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char ch = sc.next().charAt(0);
        int asciiValue = (int) ch;
        System.out.println(asciiValue);
    }
}

12. Equivalent ASCII Character Easy

Problem

Write a program to print the character corresponding to the given ASCII value.

Input Format
  • The first line contains a single integer n representing the ASCII value.
Output Format

Print the corresponding character for the given ASCII value.

Sample Input 1
65
Sample Output 1
A
Code
import java.util.Scanner;

class EquivalentAsciiCharacter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        char ch = (char) n;
        System.out.println(ch);
    }
}

13. Lowercase ASCII Value Easy

Problem

Write a program to check if the ASCII value of a character corresponds to a lowercase letter.

Input Format
  • The first line contains a single character ch.
Output Format

Print Yes if the character is a lowercase letter, otherwise print No.

Sample Input 1
g
Sample Output 1
Yes
Code
import java.util.Scanner;

class LowercaseAsciiValue {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char ch = sc.next().charAt(0);
        if (ch >= 'a' && ch <= 'z') {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

14. Uppercase ASCII Check Easy

Problem

Write a program to check if the ASCII value of a character corresponds to an uppercase letter.

Input Format
  • The first line contains a single character ch.
Output Format

Print Yes if the character is an uppercase letter, otherwise print No.

Sample Input 1
Z
Sample Output 1
Yes
Code
import java.util.Scanner;

class UppercaseAsciiCheck {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char ch = sc.next().charAt(0);
        if (ch >= 'A' && ch <= 'Z') {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

15. Number ASCII Check Easy

Problem

Write a program to check if the ASCII value of a character corresponds to a digit (0-9).

Input Format
  • The first line contains a single character ch.
Output Format

Print Yes if the character is a digit, otherwise print No.

Sample Input 1
5
Sample Output 1
Yes
Code
import java.util.Scanner;

class NumberAsciiCheck {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char ch = sc.next().charAt(0);
        if (ch >= '0' && ch <= '9') {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

16. Multiplication or Addition Easy

Problem

Write a program to check if a number is greater than 10 and either multiply or add another number based on the condition.

Input Format
  • The first line contains two integers a and b.
Output Format

If a > 10, print the result of a * b, otherwise print a + b.

Sample Input 1
12 5
Sample Output 1
60
Code
import java.util.Scanner;

class MultiplicationOrAddition {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        if (a > 10) {
            System.out.println(a * b);
        } else {
            System.out.println(a + b);
        }
    }
}

17. Positive or Negative Number Easy

Problem

Write a program to check if a number is positive or negative.

Input Format
  • The first line contains a single integer n.
Output Format

Print Positive if the number is positive, otherwise print Negative.

Sample Input 1
-5
Sample Output 1
Negative
Code
import java.util.Scanner;

class PositiveOrNegative {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n >= 0) {
            System.out.println("Positive");
        } else {
            System.out.println("Negative");
        }
    }
}

18. Absolute Value Easy

Problem

Write a program to print the absolute value of a number.

Input Format
  • The first line contains a single integer n.
Output Format

Print the absolute value of the number.

Sample Input 1
-20
Sample Output 1
20
Code
import java.util.Scanner;

class AbsoluteValue {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(Math.abs(n));
    }
}

19. Largest Number of Two Easy

Problem

Write a program to find the largest of two numbers.

Input Format
  • The first line contains two integers a and b.
Output Format

Print the largest number.

Sample Input 1
4 8
Sample Output 1
8
Code
import java.util.Scanner;

class LargestNumberOfTwo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        System.out.println(Math.max(a, b));
    }
}

20. Smallest Number of Two Easy

Problem

Write a program to find the smallest of two numbers.

Input Format
  • The first line contains two integers a and b.
Output Format

Print the smallest number.

Sample Input 1
3 7
Sample Output 1
3
Code
import java.util.Scanner;

class SmallestNumberOfTwo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        System.out.println(Math.min(a, b));
    }
}

21. Largest Number of Three Easy

Problem

Write a program to find the largest of three numbers.

Input Format
  • The first line contains three integers a, b, and c.
Output Format

Print the largest number among the three.

Sample Input 1
1 4 2
Sample Output 1
4
Code
import java.util.Scanner;

class LargestOfThree {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        System.out.println(Math.max(a, Math.max(b, c)));
    }
}

22. Smallest Number of Three Easy

Problem

Write a program to find the smallest of three numbers.

Input Format
  • The first line contains three integers a, b, and c.
Output Format

Print the smallest number among the three.

Sample Input 1
1 4 2
Sample Output 1
1
Code
import java.util.Scanner;

class SmallestOfThree {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        System.out.println(Math.min(a, Math.min(b, c)));
    }
}

Leave a Comment