If - Else decision statement in Java Examples | infoStud blogs

If - Else decision statement in Java Examples,  control statements in java with examples if...else statement example control statements in java, control statements in java, control statements in java pdf if-else statement java exercises pdf else if ladder in java. How do you write an IF ELSE statement in Java? What are decision making statements in Java explain with
If - Else decision statement in Java Examples 

In Java, decision statements play a major role. Without using decision statements make your code may be lengthy or maybe you can't achieve your goal. This post contains some examples of java if-else decision statements. These examples help you to build your basics and you will get to know how you can write or use decision statements in Java Program. So let's begin!!

Examples

Example 1: Write a program in java to print whether the number is even or odd.

Code

import java.util.Scanner;
public class evenodd {
public static void main(String args[]){
// Create a Scanner object
        Scanner sc = new Scanner(System.in);  
        System.out.println("Enter the Number");
        int Number = sc.nextInt();
        if (Number%2==0){
            System.out.println("The given number is Even");
        }
        else{
            System.out.println("The number is Odd");
        }
    }
}

Output

Enter the Number

665

The number is Odd

Example 2: Write a program in Java to find that whether the input number is divisible by 2, 3, and 5.

Code

import java.util.Scanner;
public class JavaProgram {
public static void main(String args[]){
        Scanner sc = new Scanner(System.in);  
// Create a Scanner object
        System.out.println("Enter the Number");
        int Number = sc.nextInt();
        if (Number%2==0 && Number%3==0 && Number%5==0){
            System.out.println("This Number is correct");
        }
        else{
            System.out.println("Try another Number");
        }
    }
}

Output

Enter the Number

30

This Number is correct


Enter the Number

6

Try another Number

Example 3: Write a program in java to print whether the number enter by the user is positive or not.

Code

import java.util.Scanner;
public class JavaProgram {
public static void main(String args[]){
        Scanner sc = new Scanner(System.in);  
// Create a Scanner object
        System.out.println("Enter the Number");
        int Number = sc.nextInt();
        if (Number>0 & Number ==0){
            System.out.println("This Number is positive");
        }
        else{
            System.out.println("This Number is Negative");
        }
    }
}

Output

Enter the Number

-1

This Number is Negative

Example 4: Write a program in java to print whether the student is pass or fail on the basis of percentage.

Code

import java.util.Scanner;
public class JavaProgram {
public static void main(String args[]){
        Scanner sc = new Scanner(System.in); 
 // Create a Scanner object
        System.out.println("Enter the Percentage");
        int Number = sc.nextInt();
        if (Number>=40){
            System.out.println("Student is Pass");
        }
        else{
            System.out.println("Student is fail");
        }
    }
}

Output

Enter the Percentage

30

Student is fail


Enter the Percentage

54

Student is Pass

Example 5: Write a program in java to check whether the inputs given by the user are rectangle measurements or not.

Code

import java.util.Scanner;

public class Checkrectangle {
    public static void main (String args[]){
        
        Scanner sc = new Scanner(System.in);  
// Create a Scanner object
        System.out.println("Enter the Length Measurement");
        int Length = sc.nextInt();
        System.out.println("Enter the Breadth Measurement");
        int Breadth  = sc.nextInt(); 
        if(Length>Breadth){
            int cLength*Breadth;
            System.out.println("Area of Rectangle is: "+c);
        }
        else{
            System.out.println("Please Enter the valid Dimensions 
for the Rectangle as per the values assigned");
        }
    }
}

Output

Enter the Length Measurement

40

Enter the Breadth Measurement

30

Area of Rectangle is: 1200


Enter the Length Measurement

50

Enter the Breadth Measurement

60

Please Enter the valid Dimensions for the Rectangle as per the values assigned






Post a Comment

0 Comments