Java challeges with if-else

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

 class example1 {

    public static void main(String[] args) {

        int a=-2;

        if(a>0) {

            System.out.println("A is positive");

        }

        else

            System.out.println("A is  nagative");

        }

    }

}


OUTPUT:


  


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

public class even {

public static void main (String[] args) {

    int number=11;

    if(number%2==0){

        System.out.println("Number is even");

    }else if(number%2==1){

        System.out.println("Number is odd");

    }else{

        System.out.println("Number is not zero");

    }

    }

   }

OUTPUT:




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


class largestnum {
    public static void main(String[] args){
        int a=10, b=25, c=40;
        System.out.println(" a =" +a  +"\n b ="+b +"\n c =" +c );
        if(a>=b && a>=c){
            System.out.println("a is  big");
        }else if(b>=a && b>=c){
            System.out.println("b is big");}
            else{
                System.out.println("c is big");
            }
        }
    }

OUTPUT:








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

class divisible5 {
    public static void main(String[]args) {
        int number=20;
        if(number%5==0){
            System.out.println("Num = "+number + " is divisible by 5");
        }
        else{
            System.out.println("Num = "+number + " is not divisible by 5");
        }
    }
}

OUTPUT:







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

 class divisible3 {
    public static void main(String[] args) {
        int a=15;
        if(a%3==0 && a%5==0) {
            System.out.println(" a = " +a + "is divisible by 3,5");
        }
        else{
        System. out.println("a is not divisible by 3,5");
        }
    }
}

OUTPUT:






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

import java.util.Scanner;
class age {
    public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
 int age;
 System.out.println("Enter your age:");
 age=sc.nextInt();
 if(age>=18){
    System.out.println("Person is eligible vote.");
 }else{
    System.out.println("Person is not eligible vote.");
 }
 sc.close();
    }
}

OUTPUT:







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

import java.util.Scanner;

class threedigit{

    public static void main(String[] args){

        Scanner sc=new Scanner(System.in);

        int number;

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

        number=sc.nextInt();

        if(number>=100 && number<999){

            System.out.println("Number = " +number + " is a three digit number");

        }

            else{

                System.out.println("Number = " +number + " is not a three digit number");

            }

            sc.close();

            }

        }

  OUTPUT:








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

class equals {

    public static void main(String[] args) {

        int a=10, b=20, c=10;

        System.out.println("a="+a +"\n b="+b +"\n c="+c);

        if(a==b &amp;&amp; b==c){

            System.out.println("a and b is equal");

        }else if(a==c ){

            System.out.println("a and c is equal");

        }

        else{

            System.out.println("a and c is not equal");

        }

        }

 }

OUTPUT:





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

import java.util.Scanner;

class grade {

public static void main(String[] args){

    Scanner sc=new Scanner(System.in);

    int marks,grade;

    System.out.println("Enter the marks:");

    marks=sc.nextInt();

        if(marks>=90 && marks < 100){

            System.out.println("Grage: A");

        }

        else if(marks>=65 && marks<90){

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

        }

        else if(marks>=35 && marks<65){

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

        }

        else if(marks>0 && marks<35){

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

        }

        else{

            System.out.println("Invaild marks");

        }

        sc.close();

    }

}

OUTPUT:






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

import java.util.Scanner;

public class leapyear {

    public static void main(String[] args){

        Scanner sc=new Scanner(System.in);

        int year;

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

        year=sc.nextInt();

        if(year%4==0){

            System.out.println("It is  leap year");

        }

        else{

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

        }

    }

}

OUTPUT:





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

class smallest2 {

    public static void main (String[] args) {

        Scanner sc=new Scanner(System.in);

        int a,b;

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

        a=sc.nextInt();

        System.out.println("Enter a number b:");

        b=sc.nextInt();

        if(a<b){

            System.out.println("a is small");

        }

        else{

            System.out.println("b is small");

        }

        sc.close();

    }

}

OUTPUT:







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

import java.util.Scanner;

class smallestnum {

    public static void main (String[] args){

        Scanner sc=new Scanner(System.in);

     int a,b,c;

     System.out.println("Enter the number a:");

     a=sc.nextInt();

     System.out.println("Enter the number b:");

     b=sc.nextInt();

     System.out.println("Enter the number c:");

     c=sc.nextInt();

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

        System.out.println("a is small");

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

        System.out.println("b is small");

     }

     else{

        System.out.println("c is small");

     }

    }

}

OUTPUT:




     

    

Popular posts from this blog

core java challenges

for and while challenges

html task