Write a program in java to determine whether the driver is to be insured or not
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.
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
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.
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");
}
}