If Statement examples in Java

Posted By: Java Examples - 8:47 PM

Share

& Comment

Example 1 :
If Student marks is grater than equal 60 print "passed" else "failed"

/**
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package statement;

import java.util.Scanner;

/**
 *
 * @author ACHCHUTHAN
 */
public class Result {

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your marks=");
        int m = input.nextInt();
        System.out.print("Your result : ");
        if (m >= 60 && m <= 100) {
            System.out.println("Passed");
        } else if (m >= 0 && m <= 59) {
            System.out.println("Failed");
        } else {
            System.out.println("Your marks rang is rong");
        }
    }
}

Output of this program :

run:
Enter your marks=
56
Your result : Failed
BUILD SUCCESSFUL (total time: 5 seconds)


Example 2 :
Write a program to work out how much a person pay to cinema ,Assume that the full price is Rs.200.00 ,The program should input the age and decide the following basis; under 5-free;  age 5-12-full price;  age 55 or over-free; *age limit between 0-100

/**
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package statement;

import java.util.Scanner;

/**
 *
 * @author ACHCHUTHAN
 */
public class TicketPrice {

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        int p, a;
        p = 200;
        System.out.println("Enter your age : ");
        a = input.nextInt();
        System.out.print("Your ticket price : ");
        if (a >= 55 && a <= 100) {
            System.out.println("free");
        } else if (a >= 13 && a <= 54) {
            System.out.println("Rs:" + p);
        } else if (a >= 5 && a <= 12) {
            System.out.println("Rs:" + p / 2);
        } else if (a >= 1 && a <= 11) {
            System.out.println("free");
        } else {
            System.out.println("your age range is rong");
        }
    }
}

Output of this program :

run:
Enter your age : 
21
Your ticket price : Rs:200
BUILD SUCCESSFUL (total time: 3 seconds)


Example 3 : 
Write a program to input three numbers and display them increasing numerical size

/**
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package statement;

import java.util.Scanner;

/**
 *
 * @author ACHCHUTHAN
 */
public class OrderOfNumbers {

    static public void main(String args[]) {
        Scanner input = new Scanner(System.in);
        int x, y, z;
        System.out.println("enter the value of x : ");
        x = input.nextInt();
        System.out.println("enter the value of y : ");
        y = input.nextInt();
        System.out.println("enter the value of z : ");
        z = input.nextInt();
        System.out.print("Decreasing order of three numbers : ");
        if (x > y && y > z) {
            System.out.println(x + "," + y + "," + z);
        }
        if (x > z && z > y) {
            System.out.println(x + "," + z + "," + y);
        }
        if (y > x && x > z) {
            System.out.println(y + "," + x + "," + z);
        }
        if (y > z && z > x) {
            System.out.println(y + "," + z + "," + x);
        }
        if (z > y && y > x) {
            System.out.println(z + "," + y + "," + x);
        }
        if (z > x && x > y) {
            System.out.println(z + "," + x + "," + y);
        }
    }
}

Output of this program :

run:
enter the value of x : 
12
enter the value of y : 
8
enter the value of z : 
15
Decreasing order of three numbers : 15,12,8
BUILD SUCCESSFUL (total time: 7 seconds)

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .