Java if-else Venkatesh

 

Java Challenges with if-else



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


Code :

import java.util.Scanner;

class venkat1 {

    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(num + " is Positive");

        else

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

    }

}

Output :

Enter a number: -7

-7 is Negative



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


Code :

import java.util.Scanner;

class venkat2 {

    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");

    }

}

Output :

Enter a number: 14

14 is Even


3. Write a program to find the largest of three numbers.


Code :

import java.util.Scanner;

class venkat3 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

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

        int a = sc.nextInt();

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

        int b = sc.nextInt();

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

        int c = sc.nextInt();

        if (a >= b && a >= c)

            System.out.println("Largest = " + a);

        else if (b >= a && b >= c)

            System.out.println("Largest = " + b);

        else

            System.out.println("Largest = " + c);

    }

}

Output :

Enter first number: 25

Enter second number: 42

Enter third number: 18

Largest = 42

4. Write a program to check if a number is divisible by 5.


Code :

import java.util.Scanner;

class venkat4 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

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

        int num = sc.nextInt();

        if (num % 5 == 0)

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

        else

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

    }

}

Output :

Enter a number: 40

40 is divisible by 5


5. Write a program to check if a number is divisible by 3 and 5.


Code :

import java.util.Scanner;

class venkat5 {

    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 3 and 5");

        else

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

    }

}

Output :

Enter a number: 30

30 is divisible by 3 and 5


6. Write a program to check if a person is eligible to vote (age ≥ 18).


Code :

import java.util.Scanner;

class venkat6 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

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

        int age = sc.nextInt();

        if (age >= 18)

            System.out.println("Eligible to vote");

        else

            System.out.println("Not eligible to vote");

    }

}

Output :

Enter age: 16

Not eligible to vote


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


Code :

import java.util.Scanner;

class venkat7 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

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

        int num = sc.nextInt();

        if (num >= 100 && num <= 999)

            System.out.println(num + " is a three-digit number");

        else

            System.out.println(num + " is not a three-digit number");

    }

}

Output :

Enter a number: 457

457 is a three-digit number

 


8. Write a program to check if two numbers are equal or not.


Code :

import java.util.Scanner;

class venkat8 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

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

        int a = sc.nextInt();

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

        int b = sc.nextInt();

        if (a == b)

            System.out.println("Both numbers are equal");

        else

            System.out.println("Numbers are not equal");

    }

}

Output :

Enter first number: 25

Enter second number: 30

Numbers are not equal


9. Write a program to display grade (A/B/C/F) based on marks.          

                          

Code :

import java.util.Scanner;

class venkat9 {

    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 >= 70)

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

        else if (marks >= 50)

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

        else

            System.out.println("Grade: F");

    }

}

Output :

Enter marks: 72

Grade: B

     

10. Write a program to check if a year is a leap year or not.


Code :

import java.util.Scanner;

class venkat10 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

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

        int year = sc.nextInt();

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

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

        else

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

    }

}

Output :

Enter a year: 2024

2024 is a Leap Year

         

11. Write a program to find the smallest of two numbers.


Code :

import java.util.Scanner;

class venkat11 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

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

        int a = sc.nextInt();

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

        int b = sc.nextInt();

        if (a < b)

            System.out.println("Smallest = " + a);

        else

            System.out.println("Smallest = " + b);

    }

}

Output :

Enter first number: 15

Enter second number: 20

Smallest = 15


12. Write a program to find the smallest of three numbers.         

         

Code :

import java.util.Scanner;

class venkat12 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

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

        int a = sc.nextInt();

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

        int b = sc.nextInt();

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

        int c = sc.nextInt();

        if (a <= b && a <= c)

            System.out.println("Smallest = " + a);

        else if (b <= a && b <= c)

            System.out.println("Smallest = " + b);

        else

            System.out.println("Smallest = " + c);

    }

}

Output :

Enter first number: 12

Enter second number: 7

Enter third number: 15

Smallest = 7


* * * * *

Comments

Popular posts from this blog