0% found this document useful (0 votes)
2 views

Class 18

Uploaded by

M
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Class 18

Uploaded by

M
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

iii) if else if ladder

======================
It will execute the source code based on multiple conditions.

syntax:
------
if(cond1)
{
- //code to be execute if cond1 is true
}
else if(cond2)
{
- //code to be execute if cond2 is true
}
else if(cond3)
{
- //code to be execute if cond3 is true
}
else
{
- //code to be execute if all conditions are false
}

ex:
---
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the option :");
int option=sc.nextInt();

if(option==100)
System.out.println("It is a police number");
else if(option==103)
System.out.println("It is enquiry number");
else if(option==108)
System.out.println("It is emergency number");
else
System.out.println("Invalid option");
}
}

Q) Write a java program to check given alphabet is a vowel or not?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the alphabet :");
char ch=sc.next().charAt(0);

if(ch=='a'|| ch=='A')
System.out.println("It is a vowel");
else if(ch=='e' || ch=='E')
System.out.println("It is a vowel");
else if(ch=='i' || ch=='I')
System.out.println("It is a vowel");
else if(ch=='o' || ch=='O')
System.out.println("It is a vowel");
else if(ch=='u'|| ch=='U')
System.out.println("It is a vowel");
else
System.out.println("It is not a vowel");
}
}

Q) Write a java program to check given alphabet is a uppercase letter , lowercase


letter, digit or a special symbol?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the alphabet :");
char ch=sc.next().charAt(0);

if(ch>='A' && ch<='Z')


System.out.println("It is uppercase letter");
else if(ch>='a' && ch<='z')
System.out.println("It is lowercase letter");
else if(ch>='0' && ch<='9')
System.out.println("It is digit");
else
System.out.println("It is special symbol");
}
}

Assignment
==========

Q) Write a java program to accept six marks of a student then find out
total ,average and grade?

i) If average is greater then equals to 70 then A grade.

ii) If average is greater then equals to 50 then B grade.

iii) If average is greater then equals to 35 then C gade.

iv) If average is less then 35 then failed.

iv) nested if stmt


====================
If stmt contains another if stmt is called nested if stmt.

syntax:
------
if(condition)
{
if(Condition)
{
-
- //code to be execute
-
}
}

ex:
---
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");

if(5>2)
{
System.out.println("stmt2");

if(true)
{
System.out.println("stmt3");
}

System.out.println("stmt4");
}
System.out.println("stmt5");
}
}

o/p:
stmt1
stmt2
stmt3
stmt4
stmt5

ex:
-----
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");

if(5>2)
{
System.out.println("stmt2");

if(false)
{
System.out.println("stmt3");
}

System.out.println("stmt4");
}
System.out.println("stmt5");
}
}

o/p:
stmt1
stmt2
stmt4
stmt5

ex:
---
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");

if(5>20)
{
System.out.println("stmt2");

if(true)
{
System.out.println("stmt3");
}

System.out.println("stmt4");
}
System.out.println("stmt5");
}
}

Q) Write a java program to check given number is positive or negative using nested
if stmt?

import java.util.*;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the number :");


int n=sc.nextInt();

if(n!=0)
{
if(n>0)
{
System.out.println("It is positive number ");
System.exit(0);
}

System.out.println("It is negative number");


}

}
}

Orchasp interview Question


==========================
Write a java program to calculate costs based on user input. The program
should prompt users to enter the total weight of items(in kilograms) and the
shipping destination (domestic or international). for demostic orders, the program
should charge Rs.500 for weights upto 5 kg and Rs.100 per additional kg. for
international orders, it should charge Rs.1000 for weights upto 5 kg , Rs.200 per
additional kg , and a Rs.500 surcharge for weights exceeding 10 kg. print
calculated shipping cost.

input:
total weight of items : 11
Shipping Destination : domestic

output:

1100

Explaination
------------
weight <=5 = (Rs.500)

weiht <=11 = (Rs.500 + 600)

ex:
import java.util.*;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter total weight of item :");


int weight=sc.nextInt(); // 11

System.out.println("Enter shipping destination :");


String destination=sc.next(); // domestic

if(destination.equals("domestic"))
{
if(weight<=5)
{
System.out.println(500);
}

if(weight>5)
{
System.out.println(500 + (weight-5)*100);
}
}
else if(destination.equals("international"))
{
if(weight<=5)
{
System.out.println(1000);
}
else if(weight>5 && weight<=10)
{
System.out.println(1000 + (weight-5) * 200);
}
else if(weight>10)
{
System.out.println(1000 + (weight-5) * 200 + 500);
}
}

}
}

You might also like