COMPUTER Class 9 Unit 8(Part -3)
Conditional Constructs in Java
Q1. Name the different ways to manage the flow of control in a program.
Ans: Normal flow, Bi-directional flow, Multiple branching.
(a) Bi-directional flow of control if-else statement
(b) Multiple branching of control switch case statement
Q2. Explain the following statements with their constructs:
Ans :(a) if-else
It is used to execute a set of statements when a condition is true, and execute another set of statements when the condition is false.
if(condition){
statement(s) }
else{
statement(s) }
(b) if-else-if
if(marks >= 80)
System.out.println("A");
else if(marks >= 60)
System.out.println("B");
else if(marks >= 40)
System.out.println("C");
else
System.out.println("D");
In the above example, the if-else-if construct provides a series of conditions and the one that matches gets executed while the other conditions and statements are skipped.
(c) nested if
One if statement inside the other is termed as nested if statement.
if(condition 1){
if(condition 2){
statement(s) }
}
Q3.Differentiate between if and switch statement.
Ans: The if statement works with all kinds of comparisons using a variety of relational and logical operators. The switch statement on the other hand checks only for equality of two values.
Q4.What is the purpose of switch statement in a program?
Ans:
The switch statement is a multi-branching statement where out of several options, any one gets executed and the rest are skipped.Q5.Explain ‘fall through’ with reference to a switch case statement?
Ans:
When we do not provide the break statement in switch statement, it leads to fall through, in which the flow of control moves from one case to another.Q6. In a switch case, when the switch value does not respond to any case then the execution transfers to ?
Ans: default case Q7
if(a > b) c = a;
else c = b;
It can be written as:
Ans: c= (a>b)?a:b
Q8. 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 inputs: Enter three angles: 40, 50, 90 Sample output: Right-angled triangle
import java.io.*;
class Triangle{
public static void main(String args[]) {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.println("Enter three angles:");
int a = Integer.parseInt(br.readLine());
int b = Integer.parseInt(br.readLine());
int c = Integer.parseInt(br.readLine());
int sum = a + b + c;
if(sum == 180 && a > 0 && b > 0 && c > 0) {
System.out.println("Triangle possible.");
if(a < 90 && b < 90 && c < 90)
System.out.println("Acute-angled triangle.");
else if(a == 90 || b == 90 || c == 90)
System.out.println("Right-angled triangle.");
else
System.out.println("Obtuse-angled triangle.");
} else
System.out.println("Triangle not possible.");
} }
Q9.
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 %. Otherwise calculate and displayactual loss and loss %. If the cost price and the selling price are equal, then display the message “Neither profit nor loss”.
Ans:
import java.io.*;
class Article{
public static void main(String args[]) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Cost Price = ");
double cp = Double.parseDouble(br.readLine());
System.out.print("Enter Selling Price = ");
double sp = Double.parseDouble(br.readLine());
if(sp == cp)
System.out.println("Neither profit nor loss.");
else if(sp > cp) {
double profit = sp - cp;
double pp = profit / cp * 100;
System.out.println("Profit = " + profit);
System.out.println("Profit % = " + pp);
} else
{
double loss = cp - sp;
double lp = loss / cp * 100;
System.out.println("Loss = " + loss);
System.out.println("Loss % = " + lp);
} } }
Q10.A cloth showroom has announced festival discounts and the gifts on the purchase of items, based on the total cost as given below:
TOTAL COST DISCOUNT GIFT
Up to Rs. 2000 5% Calculator
Rs. 2001 to 5000 10% School Bag
Rs. 5001 to 10000 15% Wall Clock
Above Rs. 10000 20% Wrist Watch
Write a program to input the total cost. Compute and display the amount to be paid by the customer along with the gift.
Ans:
import java.io.*;
class Showroom{
public static void main(String args[]) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Total cost: ");
double cost = Double.parseDouble(br.readLine());
double amt = 0.0;
double d = 0.0;
String gift = "";
if(cost <= 2000.0){
d = 5.0;
gift = "Calculator";
}
else if(cost <= 5000.0){
d = 10.0;
gift = "School Bag";
}
else if(cost <= 10000.0){
d = 15.0;
gift = "Wall Clock";
} else{
d = 20.0;
gift = "Wrist Watch";
}
d = d / 100 * cost;
cost = cost - d;
System.out.println("Amount to be paid: " + cost);
System.out.println("Gift: " + gift);
} }
Q11. The Simple Interest (SI) and Compound Interest (CI) of a sum (P) for a given time (T) and rate (R) can be calculated as:
SI = P × T × R / 100
CI = P((1 + R / 100)
T– 1)
Write a program to input the 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.
Ans:
import java.io.*;
class Interest{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Sum: ");
double p = Double.parseDouble(br.readLine());
System.out.print("Rate: ");
double r = Double.parseDouble(br.readLine());
System.out.print("Time: ");
double t = Double.parseDouble(br.readLine());
System.out.print("Type of Interest: ");
char i = br.readLine().charAt(0);
switch(i) {
case 'S':
case 's':
double si = p * r * t / 100;
System.out.println("Simple Interest = " + si);
break;
case 'C':
case 'c':
double ci = p * (Math.pow((1 + r / 100), t) - 1);
System.out.println("Compound Interest = " + ci);
break;
default:
System.out.println("Invalid choice!");
} } }