Skip to main content
Java Programming Beginner To Advanced Skip to main content

Posts

Write a program in java to determine whether the driver is to be insured or not

import java.util.*;
class ifelse
{
    public static void main(String args[])
    {
    Scanner sc= new Scanner(System.in);
    int a;
    char s,g;
    System.out.println("Enter the age");
    a=sc.nextInt();
    System.out.println("Enter the married status:");
    System.out.println("M For married & U For Unmarried");
    s=sc.next().charAt(0);
    System.out.println("Enter the Gender ");
    System.out.println("X For Male & Y For Female");
    g=sc.next().charAt(0);

if(s=='M' || s=='U' && a>30 && g=='X' || s=='U' && a>25 && g=='Y')
    System.out.println("Insured");
    else
    System.out.println("Not Insured");

    }
}
Output:
Enter the age
35
Enter the married status:
M For married & U For Unmarried
M
Enter the Gender
X For Male & Y For Female
X
Insured

Write a program in java leap year without using if else statement.

import java.util.*;

class leapyear
{
    public static void main(String args[])
    {
        int y;
        String x;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter any year");
        y=sc.nextInt();
        x=(y%4==0 && y%100!=0 || y%400==0)?"LeapYear":"Not Leap Year";
        System.out.println(x);

    }
}

Find the largest of three numbers without using if-else

import java.util.*;
class largest_three
{
    public static void main(String args[])
    {

        int a,b,c,x;
        Scanner sc= new Scanner(System.in);
        System.out.println("Enter three No");
        a=sc.nextInt();
        b=sc.nextInt();
        c=sc.nextInt();
        x=(c>((a>b)?a:b)?c:(a>b)?a:b);
        System.out.println(x+"is largest");
    }
}

Write a program in java find the smallest of three numbers using Ternary Operator.

//Find the smallest of three numbers without using if-else.
import java.util.*;
class smallest_three
{
    public static void main(String args[])
    {

        int a,b,c,x;
        Scanner sc= new Scanner(System.in);
        System.out.println("Enter three No");
        a=sc.nextInt();
        b=sc.nextInt();
        c=sc.nextInt();
        x=(c<((a<b)?a:b)?c:(a<b)?a:b);
        System.out.println(x+"is smallest");
    }
}

Find largest and smallest of two numbers without using if else.

import java.util.*;
class largest_smallest_twono
{
    public static void main(String args[])
    {

        int a,b,c,d,x,y;
        Scanner sc= new Scanner(System.in);
        System.out.println("Enter the two no");
        a=sc.nextInt();
        b=sc.nextInt();
        x=(a>b)?a:b;
        System.out.println(x+"is largest");
        System.out.println("Enter the two no");
        c=sc.nextInt();
        d=sc.nextInt();
        y=(c<d)?c:d;
        System.out.println(y+"is smallest");

    }
}

Comments