In this post, we will discuss some programs of if-else in java that helps to hands-free on java if-else condition after solving this program you can do any problem based on the if-else statement.

if else conditon


List of some programs

  • Find maximum between two number
  • find maximum between  three number
  • check whether a number is even or odd
  • check whether a year is a leap or not
  • check whether a number is divisible by x
  • check whether a number is negative, positive, or zero

Find maximum between two number     

class Main{
    public static void main(String [] ar){
        int a=589,b=698,big;     //variable declared
            big = a;        //by default big equal to a
        //check condition 
        if(a<b){
            //true, if b is greater than a
            big = b;
        }
        System.out.println("Bigger number is "+big);
    }
}
OUTPUT:
    Bigger number is 698

here we declare three variables a,bbig, and initialize the value in a is 589 and b are 698. the big variable has value a. then we check the condition using if statement either a or b greater than both of them. if b is greater then assign b into big and after the if statement print the big variable using System.out.println(); 


Find maximum between three number     

class Main{
    public static void main(String [] ar){
        int a=5,b=8,c=10,big;     //variable declared
        
        if (a>b && a >c){
            big =a;
        }
        else if(b>a && b>c)
            big =b;
        else
            big = c;
        System.out.println("Bigger number is "+big);
    }
}
OUTPUT:
    Bigger number is 10

here we declare four variables a,b,cbig, and initialize the value. In the first, if statement we check the case where a is greater than both b and c variables.
if it's false then check the next
if statement, where returns true when b is greater than both variables a and c. and last is else part where c is bigger automatic this part only when executes when both of upper condition stub false. after getting the big number we  print the big variable using System.out.println(); 



        



2 تعليقات

إرسال تعليق

أحدث أقدم