- Get link
- X
- Other Apps
Search This Blog
Java Programming Beginner To Advanced
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
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 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");
}
}
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");
}
}
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");
}
}
- Get link
- X
- Other Apps
Comments
Post a Comment