Banner_Ads

Interview Question-Answers | Tutorial For Beginners


tech4allsa2z:This blog provides you tutorial and most common Interview questions for beginners related to the following technology like java, python.

20 important Java programs based on conditional statements

20 important Java programs based on conditional statements, demonstrating how to use if, else, else if, and switch-case statements:



Check Even or Odd
  • Explanation: Check whether a given number is even or odd.
CODE

import java.util.Scanner;

public class EvenOdd {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int num = sc.nextInt();

        if (num % 2 == 0) {

            System.out.println(num + " is even");

        } else {

            System.out.println(num + " is odd");

        }

    }

}

Check Positive, Negative or Zero
  • Explanation: Check if a number is positive, negative, or zero.
CODE

import java.util.Scanner;

public class PositiveNegativeZero {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int num = sc.nextInt();

        if (num > 0) {

            System.out.println("The number is positive.");

        } else if (num < 0) {

            System.out.println("The number is negative.");

        } else {

            System.out.println("The number is zero.");

        }

    }

}

Find Largest of Three Numbers
  • Explanation: Find the largest number among three given numbers.
CODE

import java.util.Scanner;

public class LargestNumber {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter three numbers: ");

        int num1 = sc.nextInt();

        int num2 = sc.nextInt();

        int num3 = sc.nextInt();

        if (num1 >= num2 && num1 >= num3) {

            System.out.println(num1 + " is the largest number.");

        } else if (num2 >= num1 && num2 >= num3) {

            System.out.println(num2 + " is the largest number.");

        } else {

            System.out.println(num3 + " is the largest number.");

        }

    }

}

Check Vowel or Consonant
  • Explanation: Check whether a given character is a vowel or consonant.
CODE

import java.util.Scanner;

public class VowelConsonant {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a character: ");

        char ch = sc.next().charAt(0);

        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||

            ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {

            System.out.println(ch + " is a vowel.");

        } else {

            System.out.println(ch + " is a consonant.");

        }

    }

}

Check Leap Year
  • Explanation: Check if a given year is a leap year.
CODE

import java.util.Scanner;

public class LeapYear {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a year: ");

        int year = sc.nextInt();

        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

            System.out.println(year + " is a leap year.");

        } else {

            System.out.println(year + " is not a leap year.");

        }

    }

}

Find the Grade Based on Marks
  • Explanation: Determine the grade based on marks.
CODE

import java.util.Scanner;

public class Grade {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter marks: ");

        int marks = sc.nextInt();

        if (marks >= 90) {

            System.out.println("Grade A");

        } else if (marks >= 75) {

            System.out.println("Grade B");

        } else if (marks >= 60) {

            System.out.println("Grade C");

        } else if (marks >= 40) {

            System.out.println("Grade D");

        } else {

            System.out.println("Fail");

        }

    }

}

Check Alphabet, Digit, or Special Character
  • Explanation: Check if a character is an alphabet, digit, or special character.
CODE

import java.util.Scanner;

public class CharType {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a character: ");

        char ch = sc.next().charAt(0);

        if (Character.isAlphabetic(ch)) {

            System.out.println(ch + " is an alphabet.");

        } else if (Character.isDigit(ch)) {

            System.out.println(ch + " is a digit.");

        } else {

            System.out.println(ch + " is a special character.");

        }

    }

}

Switch Case for Days of the Week
  • Explanation: Print the name of the day based on the input number using switch.
CODE

import java.util.Scanner;

public class DayOfWeek {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number (1-7): ");

        int day = sc.nextInt();

        switch (day) {

            case 1: System.out.println("Monday"); break;

            case 2: System.out.println("Tuesday"); break;

            case 3: System.out.println("Wednesday"); break;

            case 4: System.out.println("Thursday"); break;

            case 5: System.out.println("Friday"); break;

            case 6: System.out.println("Saturday"); break;

            case 7: System.out.println("Sunday"); break;

            default: System.out.println("Invalid day!");

        }

    }

}

Simple Calculator (Switch Case)
  • Explanation: Implement a simple calculator using switch for basic operations.
CODE

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first number: ");

        double num1 = sc.nextDouble();

        System.out.print("Enter second number: ");

        double num2 = sc.nextDouble();

        System.out.print("Enter operation (+, -, *, /): ");

        char operator = sc.next().charAt(0);

        double result;

        switch (operator) {

            case '+': result = num1 + num2; break;

            case '-': result = num1 - num2; break;

            case '*': result = num1 * num2; break;

            case '/': result = num1 / num2; break;

            default:

                System.out.println("Invalid operator");

                return;

        }

        System.out.println("Result: " + result);

    }

}

 Check Armstrong Number
  • Explanation: Check if a number is an Armstrong number.
CODE

import java.util.Scanner;

public class Armstrong {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int num = sc.nextInt();

        int sum = 0, temp, remainder, digits = 0;

        temp = num;

        while (temp != 0) {

            temp /= 10;

            digits++;

        }

        temp = num;

        while (temp != 0) {

            remainder = temp % 10;

            sum += Math.pow(remainder, digits);

            temp /= 10;

        }

        if (sum == num) {

            System.out.println(num + " is an Armstrong number.");

        } else {

            System.out.println(num + " is not an Armstrong number.");

        }

    }

}

Check Strong Number
  • Explanation: Check if a number is a Strong number (factorial sum equals the number).
CODE

import java.util.Scanner;

public class StrongNumber {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int num = sc.nextInt();

        int sum = 0, temp = num;

        while (temp != 0) {

            int digit = temp % 10;

            sum += factorial(digit);

            temp /= 10;

        }

        if (sum == num) {

            System.out.println(num + " is a Strong number.");

        } else {

            System.out.println(num + " is not a Strong number.");

        }

    }

 

    public static int factorial(int n) {

        int fact = 1;

        for (int i = 1; i <= n; i++) {

            fact *= i;

        }

        return fact;

    }

}

Check Palindrome
  • Explanation: Check if a given string is a palindrome.
CODE

import java.util.Scanner;

public class Palindrome {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a string: ");

        String str = sc.nextLine();

        String reverse = new StringBuilder(str).reverse().toString();

        if (str.equals(reverse)) {

            System.out.println(str + " is a palindrome.");

        } else {

            System.out.println(str + " is not a palindrome.");

        }

    }

}

Find Maximum of N Numbers
  • Explanation: Find the largest of n numbers using conditional statements.
CODE

import java.util.Scanner;

public class MaximumOfN {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the number of elements: ");

        int n = sc.nextInt();

        int max = Integer.MIN_VALUE;

        for (int i = 0; i < n; i++) {

            System.out.print("Enter number " + (i + 1) + ": ");

            int num = sc.nextInt();

            if (num > max) {

                max = num;

            }

        }

        System.out.println("The largest number is " + max);

    }

}

Check Triangle Validity
  • Explanation: Check whether three sides form a valid triangle.
CODE

import java.util.Scanner;

public class TriangleValidity {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter three sides of a triangle: ");

        int a = sc.nextInt();

        int b = sc.nextInt();

        int c = sc.nextInt();

        if (a + b > c && a + c > b && b + c > a) {

            System.out.println("Valid triangle.");

        } else {

            System.out.println("Invalid triangle.");

        }

    }

}

Check Divisibility by 3 and 5
  • Explanation: Check if a number is divisible by both 3 and 5.
CODE

import java.util.Scanner;

public class DivisibleBy3and5 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int num = sc.nextInt();

        if (num % 3 == 0 && num % 5 == 0) {

            System.out.println(num + " is divisible by both 3 and 5.");

        } else {

            System.out.println(num + " is not divisible by both 3 and 5.");

        }

    }

}

Check Armstrong for a Range
  • Explanation: Check Armstrong numbers in a given range.
CODE

public class ArmstrongRange {

    public static void main(String[] args) {

        for (int num = 1; num <= 999; num++) {

            int sum = 0, temp = num;

            int digits = String.valueOf(num).length();

            while (temp != 0) {

                int remainder = temp % 10;

                sum += Math.pow(remainder, digits);

                temp /= 10;

            }

            if (sum == num) {

                System.out.println(num + " is an Armstrong number.");

            }

        }

    }

}

Find Day of the Week Using Switch
  • Explanation: Find the name of the day based on the number using switch.
CODE

import java.util.Scanner;

public class DayOfWeek {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter day number (1-7): ");

        int day = sc.nextInt();

        switch (day) {

            case 1: System.out.println("Monday"); break;

            case 2: System.out.println("Tuesday"); break;

            case 3: System.out.println("Wednesday"); break;

            case 4: System.out.println("Thursday"); break;

            case 5: System.out.println("Friday"); break;

            case 6: System.out.println("Saturday"); break;

            case 7: System.out.println("Sunday"); break;

            default: System.out.println("Invalid day!"); break;

        }

    }

}

Find the Largest Digit
  • Explanation: Find the largest digit in a number.
CODE

import java.util.Scanner;

public class LargestDigit {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int num = sc.nextInt();

        int largest = 0;

        while (num != 0) {

            int digit = num % 10;

            if (digit > largest) {

                largest = digit;

            }

            num /= 10;

        }

        System.out.println("Largest digit: " + largest);

    }

}

Find the Sum of Natural Numbers
  • Explanation: Find the sum of first n natural numbers.
CODE

import java.util.Scanner;

public class SumNaturalNumbers {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int n = sc.nextInt();

        int sum = (n * (n + 1)) / 2;

        System.out.println("Sum of first " + n + " natural numbers: " + sum);

    }

}

Check Divisibility by 7
  • Explanation: Check if a number is divisible by 7.
CODE

import java.util.Scanner;

public class DivisibleBySeven {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int num = sc.nextInt();

        if (num % 7 == 0) {

            System.out.println(num + " is divisible by 7.");

        } else {

            System.out.println(num + " is not divisible by 7.");

        }

    }

}

 

Post a Comment

0 Comments