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

9 TH Icse Conditional Statement in Java Ws

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

9 TH Icse Conditional Statement in Java Ws

computer applications
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Conditional Statement in Java

Multiple Choice Questions


Question 1
In a switch case, when the switch value does not respond to any case
then the execution transfers to:
1. a break statement
2. a default case
3. a loop
4. none
Question 2
Which of the following is a compound statement?
1. p=Integer.parseInt(in.readLine());
2. c=++a;
3. if(a>b) a++; b- - ;
4. a=4;
Question 3
Conition is essentially formed by using:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. All
Question 4
If(a>b)&&(a>c)), then which of the statement is true?
1. b is the smallest number
2. b is the greatest number
3. a is the greatest number
4. all of the above
Question 5
if(a>b)
c=a;
else
c=b;
It can be written as:
1. c= (b>a)?a:b;
2. c= (a!=b)?a:b;
3. c= (a>b)?b:a;
4. None
Question 6
If a, b and c are the sides of a triangle then which of the following
statement is true for: if(a!=b && a!=c && b!=c)?
1. Equilateral triangle
2. Scalene triangle
3. Isosceles triangle
4. All of the above
Question 7
Two arithmetic expressions can be compared with if statement,
using:
1. Arithmetic operator
2. Relational operator
3. Ternary operator
4. None
Question 8
Which of the following is a conditional statement?
1. if
2. goto
3. for
4. none
Question 9
Which of the following statements accomplishes 'fall through'?
1. for statement
2. switch statement
3. if-else
4. none
Question 10
A Java program executes but doesn't give the desired output. It is due
to:
1. the logical error in the program
2. the syntax error in the program
3. the run time error in the program
4. none
Answer the Following Questions
Question 1
Name the different ways to manage the flow of control in a program.
Normal flow of control, Bi-directional flow of control, Multiple
branching of control
Question 2
Mention one statement each to achieve:
1. Bi-directional flow of control
if-else statement
2. Multiple branching of control
switch statement
Question 3
Explain the following statements with their constructs:
(a) nested if
We can write an if-else statement within another if-else statement.
We call this nested if. It has the following syntax:
if (condition 1) {
if (condition 2) {
Statement a;
Statement b;
..
}
else {
Statement c;
Statement d;
..
}
}
else {
if (condition 3) {
Statement e;
Statement f;
..
}
else {
Statement g;
Statement h;
..
}
}
(b) if - else
if - else statement is used to execute one set of statements when the
condition is true and another set of statements when the condition is
false. It has the following syntax:
if (condition 1) {
Statement a;
Statement b;
..
}
else {
Statement c;
Statement d;
..
}
(c) if - else - if
if - else - if ladder construct is used to test multiple conditions and
then take a decision. It provides multiple branching of control. It has
the following syntax:
if (condition)
statement;
else if (condition)
statement;
else if (condition)
statement;
..
..
else
statement;
Question 4
Differentiate between if and switch statement.
Following are the difference between if and switch statement:
1. switch can only test for equality whereas if can test for any
Boolean expression.
2. switch tests the same expression against constant values while
if-else-if ladder can use different expression involving unrelated
variables.
3. switch expression must only evaluate to byte, short, int, char,
String or an enum. if doesn’t have such limitations.
4. A switch statement will run much faster than the equivalent
program written using the if-else-if ladder
Question 5
What is the purpose of switch statement in a program?
switch statement in a program, is used for multi-way branch. It
compares its expression to multiple case values for equality and
executes the case whose value is equal to the expression of switch. If
none of the cases match, default case is executed. If default case is
absent then none of the statements from switch are executed.
Question 6
Explain with the help of an example, the purpose of default in a
switch statement.
When none of the case values are equal to the expression of switch
statement then default case is executed. In the example below, value
of number is 4 so case 0, case 1 and case 2 are not equal to number.
Hence sopln of default case will get executed printing "Value of
number is greater than two" to the console.
int number = 4;
switch(number) {
case 0:
System.out.println("Value of number is zero");
break;
case 1:
System.out.println("Value of number is one");
break;
case 2:
System.out.println("Value of number is two");
break;
default:
System.out.println("Value of number is greater than two");
break;
}
Question 7
Is it necessary to use 'break' statement in a switch case statement?
Explain.
Use of break statement in a switch case statement is optional.
Omitting break statement will lead to fall through where program
execution continues into the next case and onwards till end of switch
statement is reached.
Question 8
Explain 'Fall through' with reference to a switch case statement.
break statement at the end of case is optional. Omitting break leads
to program execution continuing into the next case and onwards till a
break statement is encountered or end of switch is reached. This is
termed as Fall Through in switch case statement.
Question 9
What is a compound statement? Give an example.
Two or more statements can be grouped together by enclosing them
between opening and closing curly braces. Such a group of
statements is called a compound statement.
if (a < b) {
/*
* All statements within this set of braces
* form the compound statement
*/

System.out.println("a is less than b");


a = 10;
b = 20;
System.out.println("The value of a is " + a);
System.out.println("The value of b is " + b);
}

Question 10
Explain with an example the if-else-if construct.
if - else - if ladder construct is used to test multiple conditions and
then take a decision. It provides multiple branching of control. Below
is an example of if - else - if:
if (marks < 35)
System.out.println("Fail");
else if (marks < 60)
System.out.println("C grade");
else if (marks < 80)
System.out.println("B grade");
else if (marks < 95)
System.out.println("A grade");
else
System.out.println("A+ grade");

Question 11
Name two jump statements and their use.
break statement, it is used to jump out of a switch statement or a
loop. continue statement, it is used to skip the current iteration of
the loop and start the next iteration.
Question 12
Give two differences between the switch statement and the if-else
statement.
switch
 switch can only test if the expression is equal to any of its case
constants
 It is a multiple branching flow of control statement
if-else
 if-else can test for any boolean expression like less than, greater
than, equal to, not equal to, etc.
 It is a bi-directional flow of control statement

Predict the Output of the Given Snippet, when Executed


Question 1
int a=1,b=1,m=10,n=5;
if((a==1)&&(b==0))
{
System.out.println((m+n));
System.out.println((m—n));
}
if((a==1)&&(b==1))
{
System.out.println((m*n));
System. out.println((m%n));
}
Output
50
0

Explanation
First if condition is false, second if condition is true. Statements inside
the code block of second if condition are executed.
m*n => 10 * 5 => 50
m%n => 10 % 5 => 0
Question 2
int x=1,y=1;
if(n>0)
{
x=x+1;
y=y+1;
}
What will be the value of x and y, if n assumes a value (i) 1 (ii) 0?
Output
(i) n = 1
x=2
y=2

(ii) n = 0
x=1
y=1

Explanation
When n = 1, if condition is true, its code block is executed adding 1 to
both x and y. When n = 0, if condition is false so x and y retain their
original values.
Question 3
int b=3,k,r;
float a=15.15,c=0;
if(k==1)
{
r=(int)a/b;
System.out.println(r);
}
else
{
c=a/b;
System.out.println(c);
}
Output
Compile time error in the line if(k==1)
"variable k might not have been initialized"

Explanation
Assuming k to be a local variable declared inside a method, we are
using k in the if condition before initializing it i.e. before assigning any
value to k. Due to this, the above code will generate a compile time
error.
Question 4
switch (opn)
{
case 'a':
System.out.println("Platform Independent");
break;
case 'b':
System.out.println("Object Oriented");
case 'c':
System.out.println("Robust and Secure");
break;
default:
System.out.println("Wrong Input");
}
When (i) opn = 'b' (ii) opn = 'x' (iii) opn = 'a'
Output
(i) opn = 'b'
Object Oriented
Robust and Secure

Explanation
case 'b' is matched, "Object Oriented" gets printed to the console. As
there is no case statement in case 'b', program control falls through
to case 'c' printing "Robust and Secure" to the console. case 'c' has a
break statement which transfers the program control outside switch
statement.
(ii) opn = 'x'
Wrong Input

Explanation
None of the 3 cases match so default case is executed.
(ii) opn = 'a'
Platform Independent
Explanation
case 'a' is matched, "Platform Independent" gets printed to the
console. break statement in case 'a' transfers the program control
outside switch statement.

Correct the errors in the given programs


Question 1
class public
{
public static void main(String args{})
{
int a=45,b=70,c=65.45;
sum=a+b;
diff=c-b;
System.out.println(sum,diff);
}
}
Explanation
1. public is a keyword so it can't be used as an identifier for
naming the class. Change the class name from public to any
valid identifier, for example class Sample
2. Argument of main method is an array of Strings. Use square
brackets instead of curly brackets — String args[]
3. c is an int variable. We cannot assign a double literal 65.45 to it.
4. Variables sum & diff are not defined
5. The line System.out.println(sum,diff); should be written like this
System.out.println(sum + " " + diff);
Corrected Program
class Sample //1st correction
{
public static void main(String args[]) //2nd correction
{
int a=45,b=70,c=65; //3rd correction
int sum=a+b; //4th Correction
int diff=c-b;
System.out.println(sum + " " + diff); //5th Correction
}
}
Question 2
class Square
{
public static void main(String args[])
{
int n=289,r;
r=sqrt(n);
if(n==r)
System.out.println("Perfect Square");
else
System.out.println("Not a Perfect Square");
}
}
Explanation
1. Variable r must be of double type as Math.sqrt method returns
a double value.
2. The line r=sqrt(n); should be r=Math.sqrt(n);
Corrected Program
class Square
{
public static void main(String args[])
{
int n=289;
double r=Math.sqrt(n); //1st & 2nd correction
if(n==r)
System.out.println("Perfect Square");
else
System.out.println("Not a Perfect Square");
}
}
Question 3
class Simplify
{
public static void main(String args[])
{
int a,b,c,d;
a=10,b=5,c=1,d=2;
c=a2+b2;
d=(a+b)2;
p=c/d;
System.out.println(c + " "+ " "+d+ " "+p);
}
}

Explanation
1. The line a=10,b=5,c=1,d=2; generates a compile time error. We
will combine the declaration and initialization of these
variables.
2. The line c=a2+b2; is written in Java like this c = (int)(Math.pow(a,
2) + Math.pow(b, 2));
3. The line d=(a+b)2; is written in Java like this
d=(int)Math.pow((a+b), 2);
4. Variable p is not defined
Corrected Program
class Simplify
{
public static void main(String args[])
{
int a=10,b=5,c=1,d=2; //1st correction
c = (int)(Math.pow(a, 2) + Math.pow(b, 2)); //2nd correction
d = (int)Math.pow((a+b), 2); //3rd correction
int p=c/d; //4th correction
System.out.println(c + " "+ " "+d+ " "+p);
}
}
Question 4
class Sample
{
public static void main(String args[])
{
int n,p;
float k,r;
n=25;p=12;
if(n=25)
{
k=pow(p,2)
System.out.println("The value of"+p+ " = "+k);
}
else
{
r=Math.square root(n);
System.out.println("The value of"+n+ " = "+r);
}
}
}
Explanation
1. The line if(n=25) should be if(n==25)
2. The line k=pow(p,2) should be k=(float)Math.pow(p,2);
3. The line r=Math.square root(n); should be
r=(float)Math.sqrt(n);
Corrected Program
class Sample
{
public static void main(String args[])
{
int n,p;
float k,r;
n=25;p=12;
if(n==25) //1st correction
{
k=(float)Math.pow(p,2); //2nd correction
System.out.println("The value of"+p+ " = "+k);
}
else
{
r=(float)Math.sqrt(n); //3rd correction
System.out.println("The value of"+n+ " = "+r);
}
}
}

Solutions to Unsolved Java Programs


Question 1
Write a program to input three angles of a triangle and check
whether a triangle is possible or not. If possible then check whether
it is an acute-angled triangle, right-angled or an obtuse-angled
triangle otherwise, display 'Triangle not possible'.
Sample Input: Enter three angles: 40, 50, 90
Sample Output: Right=angled Triangle

import java.util.Scanner;
public class TriangleAngle
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first angle: ");
int a1 = in.nextInt();
System.out.print("Enter second angle: ");
int a2 = in.nextInt();
System.out.print("Enter third angle: ");
int a3 = in.nextInt();
int angleSum = a1 + a2 + a3;
if (angleSum == 180 && a1 > 0 && a2 > 0 && a3 > 0) {
if (a1 < 90 && a2 < 90 && a3 < 90) {
System.out.println("Acute-angled Triangle");
}
else if (a1 == 90 || a2 == 90 || a3 == 90) {
System.out.println("Right-angled Triangle");
}
else {
System.out.println("Obtuse-angled Triangle");
}
}
else {
System.out.println("Triangle not possible");
}
}
}

Question 2
Write a program to input the cost price and the selling price of an
article. If the selling price is more than the cost price then calculate
and display actual profit and profit per cent otherwise, calculate and
display actual loss and loss per cent. If the cost price and the selling
price are equal, the program displays the message 'Neither profit nor
loss'.
import java.util.Scanner;
public class Profit
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter cost price of the article: ");
double cp = in.nextDouble();
System.out.print("Enter selling price of the article: ");
double sp = in.nextDouble();
double pl = sp - cp;
double percent = Math.abs(pl) / cp * 100;
if (pl > 0) {
System.out.println("Profit = " + pl);
System.out.println("Profit % = " + percent);
}
else if (pl < 0) {
System.out.println("Loss = " + Math.abs(pl));
System.out.println("Loss % = " + percent);
}
else {
System.out.println("Neither profit nor loss");
}
}
}
Question 3
Write a program to input three numbers and check whether they are
equal or not. If they are unequal numbers then display the greatest
among them otherwise, display the message 'All the numbers are
equal'.
Sample Input: 34, 87, 61
Sample Output: Greatest number: 87
Sample Input: 81, 81, 81
Sample Output: All the numbers are equal.

import java.util.Scanner;
public class Numbers
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
System.out.print("Enter third number: ");
int c = in.nextInt();
if (a == b && b == c) {
System.out.println("All the numbers are equal");
}
else {
int g = a;
if (b > g)
g = b;
if (c > g)
g = c;
System.out.println("Greatest number: " + g);
}
}
}

Question 4
Write a program to accept a number and check whether the number
is divisible by 3 as well as 5. Otherwise, decide:
(a) Is the number divisible by 3 and not by 5?
(b) Is the number divisible by 5 and not by 3?
(c) Is the number neither divisible by 3 nor by 5?
The program displays the message accordingly.

import java.util.Scanner;
public class Divisor
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();
if (num % 3 == 0 && num % 5 == 0)
System.out.println("Divisible by 3 and 5");
else if (num % 3 == 0)
System.out.println("Divisible by 3 but not by 5");
else if (num % 5 == 0)
System.out.println("Divisible by 5 but not by 3");
else
System.out.println("Neither divisible by 3 nor by 5");
}
}

Question 5
Write a program to input year and check whether it is:
(a) a Leap year (b) a Century Leap year (c) a Century year but not a
Leap year
Sample Input: 2000
Sample Output: It is a Century Leap Year.

import java.util.Scanner;
public class CenturyLeapYear
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the year to check: ");
int yr = in.nextInt();

if (yr % 4 == 0 && yr % 100 != 0)


System.out.println("It is a Leap Year");
else if (yr % 400 == 0)
System.out.println("It is a Century Leap Year");
else if (yr % 100 == 0)
System.out.println("It is a Century Year but not a Leap Year");
else
System.out.println("It is neither a Century Year nor a Leap Year");
}
}

Question 6
Write a program to input two unequal positive numbers and check
whether they are perfect square numbers or not. If the user enters a
negative number then the program displays the message 'Square
root of a negative number can't be determined'.
Sample Input: 81, 100
Sample Output: They are perfect square numbers.
Sample Input: 225, 99
Sample Output: 225 is a perfect square number.
99 is not a perfect square number.
import java.util.Scanner;
public class PerfectSquare
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
if (a < 0 || b < 0) {
System.out.println("Square root of a negative number can't be
determined");
}
else {
double sqrtA = Math.sqrt(a);
double sqrtB = Math.sqrt(b);
boolean isAPerfectSq = sqrtA - Math.floor(sqrtA) == 0;
boolean isBPerfectSq = sqrtB - Math.floor(sqrtB) == 0;
if (isAPerfectSq && isBPerfectSq) {
System.out.println("They are perfect square numbers.");
}
else if (isAPerfectSq) {
System.out.println(a + " is a perfect square number.");
System.out.println(b + " is not a perfect square number.");
}
else if (isBPerfectSq) {
System.out.println(a + " is not a perfect square number.");
System.out.println(b + " is a perfect square number.");
}
else {
System.out.println("Both are not perfect square numbers.");
}
}
}
}

Question 7
Without using if-else statement and ternary operators, accept three
unequal numbers and display the second smallest number.
[Hint: Use Math.max( ) and Math.min( )]
Sample Input: 34, 82, 61
Sample Output: 61

import java.util.Scanner;
public class SecondSmallestNumber
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 3 unequal numbers");
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
System.out.print("Enter third number: ");
int c = in.nextInt();
int sum = a + b + c;
int big = Math.max(a, b);
big = Math.max(big, c);
int small = Math.min(a, b);
small = Math.min(small, c);
int result = sum - big - small;
System.out.println("Second Smallest Number = " + result);
}
}

Question 8
Write a program to input three unequal numbers. Display the
greatest and the smallest number.
Sample Input: 28, 98, 56
Sample Output: Greatest Number: 98
Smallest Number: 28
import java.util.Scanner;
public class MinMaxNumbers
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 3 unequal numbers");
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
System.out.print("Enter third number: ");
int c = in.nextInt();
int min = a, max = a;
min = b < min ? b : min;
min = c < min ? c : min;
max = b > max ? b : max;
max = c > max ? c : max;
System.out.println("Greatest Number: " + max);
System.out.println("Smallest Number: " + min);
}
}

Question 9
import java.util.Scanner;
public class PrePaidTaxi
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Taxi Number: ");
String taxiNo = in.nextLine();
System.out.print("Enter distance travelled: ");
int dist = in.nextInt();

int fare = 0;
if (dist <= 5)
fare = 100;
else if (dist <= 15)
fare = 100 + (dist - 5) * 10;
else if (dist <= 25)
fare = 100 + 100 + (dist - 15) * 8;
else
fare = 100 + 100 + 80 + (dist - 25) * 5;
System.out.println("Taxi No: " + taxiNo);
System.out.println("Distance covered: " + dist);
System.out.println("Amount: " + fare);

}
}
Question 10
import java.util.Scanner;
public class ClothDiscount
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter total cost: ");
double cost = in.nextDouble();
String gift;
double amt;
if (cost <= 2000.0) {
amt = cost - (cost * 5 / 100);
gift = "Calculator";
}
else if (cost <= 5000.0) {
amt = cost - (cost * 10 / 100);
gift = "School Bag";
}
else if (cost <= 10000.0) {
amt = cost - (cost * 15 / 100);
gift = "Wall Clock";
}
else {
amt = cost - (cost * 20 / 100);
gift = "Wrist Watch";
}
System.out.println("Amount to be paid: " + amt);
System.out.println("Gift: " + gift);
}
}

Question 11
import java.util.Scanner;
public class IncomeTax
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = in.nextLine();
System.out.print("Enter age: ");
int age = in.nextInt();
System.out.print("Enter taxable income: ");
double ti = in.nextDouble();
double tax = 0.0;

if (age > 60) {


System.out.print("Wrong Category");
}
else {
if (ti <= 250000)
tax = 0;
else if (ti <= 500000)
tax = (ti - 160000) * 10 / 100;
else if (ti <= 1000000)
tax = 34000 + ((ti - 500000) * 20 / 100);
else
tax = 94000 + ((ti - 1000000) * 30 / 100);
}
System.out.println("Name: " + name);
System.out.println("Tax Payable: " + tax);
}
}

Question 12
import java.util.Scanner;
public class TermDeposit
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter sum of money: ");
double sum = in.nextDouble();
System.out.print("Enter number of days: ");
int days = in.nextInt();
double interest = 0.0;
if (days <= 180)
interest = sum * 5.5 / 100.0;
else if (days <= 364)
interest = sum * 7.5 / 100.0;
else if (days == 365)
interest = sum * 9.0 / 100.0;
else
interest = sum * 8.5 / 100.0;
double amt = sum + interest;
System.out.print("Maturity Amount = " + amt);
}
}

Question 13
import java.util.Scanner;
public class LICPolicy
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = in.nextLine();
System.out.print("Enter Sum Assured: ");
double sum = in.nextDouble();
System.out.print("Enter First Premium: ");
double pre = in.nextDouble();
double disc = 0.0, comm = 0.0;

if(sum <= 100000){


disc = pre * 5.0 / 100.0;
comm = sum * 2.0 / 100.0;
}
else if(sum <= 200000){
disc = pre * 8.0 / 100.0;
comm = sum * 3.0 / 100.0;
}
else if(sum <= 500000){
disc = pre * 10.0 / 100.0;
comm = sum * 5.0 / 100.0;
}
else{
disc = pre * 15.0 / 100.0;
comm = sum * 7.5 / 100.0;
}

System.out.println("Name of the policy holder: " + name);


System.out.println("Sum assured: " + sum);
System.out.println("Premium: " + pre);
System.out.println("Discount on the first premium: " + disc);
System.out.println("Commission of the agent: " + comm);

}
}

Question 14

import java.util.Scanner;
public class Salary
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter name: ");
String name = in.nextLine();
System.out.print("Enter basic salary: ");
double bs = in.nextDouble();
double da = 0.0, sa = 0.0;

if (bs <= 10000){


da = bs * 10.0 / 100.0;
sa = bs * 5.0 / 100.0;
}
else if (bs <= 20000){
da = bs * 12.0 / 100.0;
sa = bs * 8.0 / 100.0;
}
else if (bs <= 30000){
da = bs * 15.0 / 100.0;
sa = bs * 10.0 / 100.0;
}
else{
da = bs * 20.0 / 100.0;
sa = bs * 12.0 / 100.0;
}
double gs = bs + da + sa;
System.out.println("Name\tBasic\tDA\tSpl. Allowance\tGross
Salary");
System.out.println(name + "\t" + bs + "\t" + da + "\t" + sa + "\t" + gs);
}
}

Question 15
Using a switch case statement, write a menu driven program to
convert a given temperature from Fahrenheit to Celsius and vice-
versa. For an incorrect choice, an appropriate message should be
displayed.
Hint: c = 5/9*(f-32) and f=1.8*c+32

import java.util.Scanner;
public class Temperature
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 to convert from Fahrenheit to Celsius");
System.out.println("Type 2 to convert from Celsius to Fahrenheit");
int choice = in.nextInt();
double ft = 0.0, ct = 0.0;

switch (choice) {
case 1:
System.out.print("Enter temperature in Fahrenheit: ");
ft = in.nextDouble();
ct = 5 / 9.0 * (ft - 32);
System.out.println("Temperature in Celsius: " + ct);
break;

case 2:
System.out.print("Enter temperature in Celsius: ");
ct = in.nextDouble();
ft = 1.8 * ct + 32;
System.out.println("Temperature in Fahrenheit: " + ft);
break;

default:
System.out.println("Incorrect Choice");
break;
}
}
}

Question 16
The volume of solids, viz. cuboid, cylinder and cone can be calculated
by the formula:
1. Volume of a cuboid (v = l*b*h)
2. Volume of a cylinder (v = π*r2*h)
3. Volume of a cone (v = (1/3)*π*r2*h)
Using a switch case statement, write a program to find the volume of
different solids by taking suitable variables and data types.

import java.util.Scanner;
public class MenuVolume
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Volume of cuboid");
System.out.println("2. Volume of cylinder");
System.out.println("3. Volume of cone");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
switch(choice) {
case 1:
System.out.print("Enter length of cuboid: ");
double l = in.nextDouble();
System.out.print("Enter breadth of cuboid: ");
double b = in.nextDouble();
System.out.print("Enter height of cuboid: ");
double h = in.nextDouble();
double vol = l * b * h;
System.out.println("Volume of cuboid = " + vol);
break;
case 2:
System.out.print("Enter radius of cylinder: ");
double rCylinder = in.nextDouble();
System.out.print("Enter height of cylinder: ");
double hCylinder = in.nextDouble();
double vCylinder = (22 / 7.0) * Math.pow(rCylinder, 2) * hCylinder;
System.out.println("Volume of cylinder = " + vCylinder);
break;
case 3:
System.out.print("Enter radius of cone: ");
double rCone = in.nextDouble();
System.out.print("Enter height of cone: ");
double hCone = in.nextDouble();
double vCone = (1 / 3.0) * (22 / 7.0) * Math.pow(rCone, 2) * hCone;
System.out.println("Volume of cone = " + vCone);
break;
default:
System.out.println("Wrong choice! Please select from 1 or 2 or 3.");
}
}
}

Question 17
A Mega Shop has different floors which display varieties of dresses as
mentioned
below:
1. Ground floor : Kids Wear
2. First floor : Ladies Wear
3. Second floor : Designer Sarees
4. Third Floor : Men's Wear
The user enters floor number and gets the information regarding
different items of the Mega shop. After shopping, the customer pays
the amount at the billing counter and the shopkeeper prints the bill
in the given format:
Name of the Shop: City Mart
Total Amount:
Visit Again!!
Write a program to perform the above task as per the user's choice.

import java.util.Scanner;
public class MegaShop
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Ground floor");
System.out.println("2. First floor");
System.out.println("3. Second floor");
System.out.println("4. Third floor");

System.out.print("Select a floor: ");


int floor = in.nextInt();

switch (floor) {
case 1:
System.out.println("Kids Wear");
break;
case 2:
System.out.println("Ladies Wear");
break;
case 3:
System.out.println("Designer Sarees");
break;
case 4:
System.out.println("Men's Wear");
break;
default:
System.out.println("Incorrect Floor");
break;
}
System.out.print("Enter bill amount: ");
double amt = in.nextDouble();
System.out.println("Name of the Shop: City Mart");
System.out.println("Total Amount: " + amt);
System.out.println("Visit Again!!");
}
}
}

Question 18
The equivalent resistance of series and parallel connections of two
resistances are given by the formula:
(a) R1 = r1 + r2 (Series)
(b) R2 = (r1 * r2) / (r1 + r2) (Parallel)
Using a switch case statement, write a program to enter the value of
r1 and r2. Calculate and display the equivalent resistances accordingly.

import java.util.Scanner;
public class Resistance
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Series");
System.out.println("2. Parallel");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter r1: ");
double r1 = in.nextDouble();
System.out.print("Enter r2: ");
double r2 = in.nextDouble();
double eqr = 0.0;
switch (choice) {
case 1:
eqr = r1 + r2;
System.out.println("Equivalent resistance = " + eqr);
break;
case 2:
eqr = (r1 * r2) / (r1 + r2);
System.out.println("Equivalent resistance = " + eqr);
break;
default:
System.out.println("Incorrect choice");
break;
}
}
}

Question 19
The Simple Interest (SI) and Compound Interest (CI) of a sum (P) for a
given time (T) and rate (R) can be calculated as:
(a) SI = (p * r * t) / 100 (b) CI = P * ((1 + (R / 100))T - 1)
Write a program to input sum, rate, time and type of Interest ('S' for
Simple Interest and 'C' for Compound Interest). Calculate and display
the sum and the interest earned.

import java.util.Scanner;
public class Interest
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the sum: ");
double p = in.nextDouble();
System.out.print("Enter rate: ");
double r = in.nextDouble();
System.out.print("Enter time: ");
int t = in.nextInt();
System.out.println("Enter type of Interest");
System.out.print("('S'- Simple Interest 'C'- Compound Interest): ");
char type = in.next().charAt(0);
boolean isTypeValid = true;

double interest = 0.0;

switch (type) {
case 'S':
interest = p * r * t / 100;
break;

case 'C':
interest = p * (Math.pow((1 + (r / 100)), t) - 1);
break;

default:
isTypeValid = false;
System.out.println("Incorrect Interest type");
break;
}

if (isTypeValid) {
double amt = p + interest;
System.out.println("Sum = " + p);
System.out.println("Interest = " + interest);
System.out.println("Sum + Interest = " + amt);
}
}
}

Question 20

import java.util.Scanner;
public class ElectronicsSale
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = in.nextLine();
System.out.print("Enter Amount of Purchase: ");
double amt = in.nextDouble();
System.out.println("Enter Type of Purchase");
System.out.print("'L'- Laptop or 'D'- Desktop: ");
char type = in.next().charAt(0);
type = Character.toUpperCase(type);
double disc = 0.0;
if (amt <= 25000)
disc = type == 'L' ? 0.0 : 5.0;
else if (amt <= 50000)
disc = type == 'L' ? 5.0 : 7.0;
else if (amt <= 100000)
disc = type == 'L' ? 7.5 : 10.0;
else
disc = type == 'L' ? 10.0 : 15.0;
double netAmt = amt - (disc * amt / 100);
System.out.println("Name: " + name);
System.out.println("Net Amount: " + netAmt);
}

You might also like