0% found this document useful (0 votes)
700 views43 pages

Chapter 7 - Conditional Statements in Java

The document discusses conditional statements in Java with multiple choice and open-ended questions. It covers if, else-if, switch statements and their usage along with examples. Key topics covered include conditional logic, relational operators, default case in switch, break in switch, and fall through.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
700 views43 pages

Chapter 7 - Conditional Statements in Java

The document discusses conditional statements in Java with multiple choice and open-ended questions. It covers if, else-if, switch statements and their usage along with examples. Key topics covered include conditional logic, relational operators, default case in switch, break in switch, and fall through.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 43

Chapter 7- Conditional Statements 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

Condition 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.

Answer

1. Normal flow of control


2. Bi-directional flow of control
3. Multiple branching of control

Question 2

Mention one statement each to achieve:


(a) Bi-directional flow of control

Answer: if-else statement

(b) Multiple branching of control

Answer: switch statement

Question 3
Explain the following statements with their constructs:
(a) nested if
Answer: The ‘ if ‘ statement placed within another ‘ if ’ statement is known as Nested if statement. It has the following syntax:

if(condition)
{
if(condition)
{
Statement;
}
else
{
statement;
}
}
else
{
if(condition)
{
Statement;
}
else
{
statement;
}
}

(b) if - else

Answer: It is a logical situation in which either of the two actions are to be performed depending upon specific condition. It executes one set of statements
when the condition is true and another set of statements when the condition is false. It has the following syntax/construct:
if(condition)
{
Statement;
}
else
{
Statement;
}

(c) if - else - if

Answer: It is a logical situation in which more than two actions are to be performed depending upon specific conditions. It executes 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)
Statement;
else if(condition)
Statement;
else
Statement;

Question 4

Differentiate between if and switch statement.

Answer:

if switch
1. It is a bi-directional flow of control. It is a multiple branching statement.
2. It results in boolean type value. It results in integer type value.
3. It does not use break statement to terminate. It uses break statement to terminate the case.

Question 5

What is the purpose of switch statement in a program?

Answer: The switch statement is a multiple branching statement which is used to execute a particular block of statements out of a number of blocks as per
the user’s choice.

Question 6

Explain with the help of an example, the purpose of default in a switch statement.
Answer: The statement written in the default case executes only when switch value doesn’t match with any case number.
Example:
switch(n)
{
case 1;
System.out.println(“Good Morning”);
break;
case 2;
System.out.println(“Good Afternoon”);
break;
case 3;
System.out.println(“Good Evening”);
break;
default;
System.out.println(“Wrong Input”);
}

In the example above, if user enters the value of choice variable ‘n’ as 4, then in the switch, case 1, 2 and 3 doesn’t match with its value.
Hence default case statement will be executed and output will be “Wrong Input”.

Question 7
Is it necessary to use 'break' statement in a switch case statement? Explain.

Answer: Yes, it’s necessary to use ‘break’ statement in a switch case statement. Because, it forces the control to exit from the switch block. Omitting break
statement will lead to fall through where control enters into another case for execution.

Question 8
Explain 'Fall through' with reference to a switch case statement.

Answer: If the ‘break’ statement is not used after a case then the control enters into another case for execution. This situation or condition is called fall
through.
Example:
int n = 1;
switch(n)
{
case 1:
System.out.println("Laptop");
case 2:
System.out.println("Desktop");
break;
default:
System.out.println("Wrong Input");
}

Output: Laptop
Desktop

Question 9

What is a compound statement? Give an example.

Answer: A statement that includes a set of statements within the opening and closing curly braces is called a compound statement.
Example:

if (a < b)
{
sq=a*a;
cb=b*b*b;
}

Question 10

Explain with an example the if-else-if construct.

Answer: It is a logical situation in which more than two actions are to be performed depending upon specific conditions. It executes one set of statements
when the condition is true and another set of statements when the condition is false.
Example:
if(a>b)
System.out.println(" a is smaller");
else if(b>a)
System.out.println("b is smaller");
else
System.out.println("both are equal");

Question 11

What is the significance of System.exit(0)?


Answer: This statement is used to terminate the execution in the middle of the program and sends the control directly to end of the program if your
requirement is fulfilled and the remaining statements will be ignored.
Example: if(n<0)
{
System.out.println(“It’s a negative number and Program Terminates");
System.exit(0);
}
double sq=Math.sqrt(n);
System.out.println(“Square root=”+sq);
In the example above, if user will enter the value of variable ‘n’ in negative then System.exit(0) statement will terminate the
program execution because square root of a negative number will not be calculated.

Question 12

Give two differences between the switch statement and the if-else statement.

Answer: Switch
1. It’s a multiple branching statement.
2. It results in integer type value.
3. It uses break statement to terminate a case.

if-else
1. It is bi-directional flow of control.
2. It results in boolean type value.
3. It does not use break statement.

III. 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));
}

Answer: Output: 50
0

(Explanation below is just to understand don’t write it)


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?

Answer: Output: (i) n = 1 then x = 2, y =2.


(ii) n = 0 then x = 1, y =1.
(Explanation below is just to understand don’t write it)
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);
}
Answer: Output: Compile time error in the line if(k==1) "variable k might not have been initialized" and also the statement ‘float a=15.15’ should be
written ‘ float a=15.15f ’.
If we assume k=1, then output will be 5. If k=0 or any other value then output will be 5.05.

(Explanation below is just to understand don’t write it)


The variable k is used in the if condition before initializing it (without assigning any value to k). Due to this, the above code will generate a compile time
error, and also ‘ float a=15.15 ’ should be ‘float a=15.15f ’.

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'

Answer: (i) Output: Object Oriented


Robust and Secure

(Explanation below is just to understand don’t write it)


case 'b' is matched, "Object Oriented" gets printed to the console. As there is no break statement in case 'b', program control jumps (falls through) to
case 'c' and also printing "Robust and Secure" to the console. case 'c' has a break statement which transfers the program control outside switch
statement.
Answer: (ii) Output: Wrong Input
(Explanation below is just to understand don’t write it)
None of the 3 cases match so default case is executed.

Answer: (iii) Output: Platform Independent


(Explanation below is just to understand don’t write it)
case 'a' is matched, "Platform Independent" gets printed to the console. break statement in case 'a' transfers the program control outside switch
statement.

1V. 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);
}
}

Answer: 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
}
}

(Explanation below is just to understand don’t write it)

1. public is a keyword so it can't be used as a class name. 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 properly use data type ‘int’ before both of them.
5. The line System.out.println(sum,diff); should be written like this System.out.println(sum + "," + diff);

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");
}
}

Answer: Corrected Program


class Square
{
public static void main(String args[])
{
int n=289;
double r = Math.sqrt(n); //1st & 2nd correction
if(n==(int)r) //3rd correction
System.out.println("Perfect Square");
else
System.out.println("Not a Perfect Square");
}
}

(Explanation below is just to understand don’t write it)

1. Variable r must be of double type as Math.sqrt method returns a double value.


2. The line r = sqrt(n); should be double r = Math.sqrt(n);
3. The Statement if(n==r) should be if(n==(int)r)
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);
}
}

Answer: Corrected Program


class Simplify
{
public static void main(String args[ ])
{
int a=10, b=5, c=0; //1st correction
double d=0.0; //2nd correction
c = a*a + b*b; //3rd correction
d = Math.pow((a+b), 2); //4th correction
double p=c/d; //5th correction
System.out.println(c + " "+ " "+d+ " "+p);
}
}

(Explanation below is just to understand don’t write it)

1. The line a=10,b=5,c=1,d=2; generates a compile time error. Also variable ‘c’ should be initialized ‘0’. And variable ‘d’ should be declared as double
type separately and initialized ‘0.0’. We will combine the declaration and initialization of these variables and will write as
int a=10,b=5,c=0;
double d=0.0;
2. The line c=a2+b2; should be written in java form like this c=a*a + b*b;
3. The line d=(a+b)2; should be written like this in java form: d=Math.pow((a+b),2);
4. Variable p is not defined and must be written like this: double p=c/d;
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);
}
}
}

Answer: Corrected Program


class Sample
{
public static void main(String args[ ])
{
int n, p;
double k, r; //1st correction
n=25; p=12;
if(n==25) //2nd correction
{
k=Math.pow(p,2); //3rd correction
System.out.println("The Power of"+p+ " = "+k); //4th correction
}
else
{
r=Math.sqrt(n); //5th correction
System.out.println("The Square root of"+n+ " = "+r); //6th correction
}
}
}

(Explanation below is just to understand don’t write it)

1. The line float k, n; should be double k, n;


2. The line if(n=25) should be if(n==25)
3. The line k=pow(p,2) should be k=Math.pow(p,2);
4. The line “The value of” should be “The Power of”;
5. The line r=Math.square root(n); should be r=Math.sqrt(n);
6. The line “The value of” should be “The Square root of”;

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;
class P1
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a,b,c,sum=0;
System.out.println("Enter three angles");
a=in.nextInt();
b=in.nextInt();
c=in.nextInt();
sum=a+b+c;
if(sum==180)
{
System.out.println("Triangle is possible");
if(a<90 && b<90 && c<90)
System.out.println("Acute angled triangle");
if(a>90 || b>90 || c>90)
System.out.println("Obtuse angled triangle");
if(a==90|| b==90 || c==90)
System.out.println("Right angled triangle");
}
else
System.out.println("Triangle is 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;
class P2
{
public static void main()
{
Scanner in=new Scanner(System.in);
int cp,sp,pro=0,los=0;
double pper=0.0,lper=0.0;
System.out.println("Enter cost price");
cp=in.nextInt();
System.out.println("Enter sale price");
sp=in.nextInt();
if(sp>cp)
{
pro=sp-cp;
pper=(pro/cp)*100;
System.out.println("Actual Profit=Rs."+pro);
System.out.println("Profit percent="+pper+"%");
}
else if(cp>sp)
{
los=cp-sp;
lper=(los/cp)*100;
System.out.println("Actual Loss=Rs."+los);
System.out.println("Loss percent="+lper+"%");
}
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;
class P3
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a,b,c;
System.out.println("Enter three numbers");
a=in.nextInt();
b=in.nextInt();
c=in.nextInt();
if(a!=b && b!=c && c!=a)
{

if( a > b && a > c)


System.out.println("Greatest number:"+a);
else if (b > a && b > c)
System.out.println("Greatest number:"+b);
else
System.out.println("Greatest number:"+c);
}
else
System.out.println("All numbers are equal");
}
}
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;
class P4
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n;
System.out.println("Enter Number");
n=in.nextInt();
if(n%3==0 && n%5==0)
System.out.println(n+" is divisible by 3 as well as 5");
else if(n%3==0 && n%5!=0)
System.out.println(n+" is divisible by 3 and not by 5");
else if(n%3!=0 && n%5==0)
System.out.println(n+" is divisible by 5 and not by 3");
else
System.out.println(n+" is 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;
class P5
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int year;
System.out.println("Enter year");
year=in.nextInt();
if(year % 4 == 0)
{
if( year % 100 == 0)
{
if( year % 400 == 0)
System.out.println(year + " is a century leap year.");
else
System.out.println(year + " is a century year but not a leap year.");
}
else
System.out.println(year + " is a leap year.");
}
else
System.out.println(year + " is not 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;
class P6
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int n1,n2,sq1,sq2;
System.out.println("Enter first number");
n1=in.nextInt();
System.out.println("Enter second number");
n2=in.nextInt();
if(n1>0 && n2>0)
{
sq1=(int) Math.sqrt(n1);
sq2=(int) Math.sqrt(n2);
if((sq1*sq1 == n1) && (sq2*sq2 == n2))
System.out.println("Both are perfect square numbers");
else if((sq1*sq1 == n1) && (sq2*sq2 != n2))
{
System.out.println(n1+" is a perfect square number");
System.out.println(n2+" is not a perfect square number");
}
else if((sq1*sq1 != n1) && (sq2*sq2 == n2))
{
System.out.println(n1+" is a perfect square number");
System.out.println(n2+" is not a perfect square number");
}
else
{
System.out.println("Both are not Perfect numbers");
}
}
else
System.out.println("Square root of negative number can't be determined");
}
}

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;
class P7
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a,b,c,max,min,sum,msum,ssn;
System.out.println("Enter three numbers");
a=in.nextInt();
b=in.nextInt();
c=in.nextInt();
max = Math.max(Math.max(a,b),c);
min = Math.min(Math.min(a,b),c);
sum = a + b + c;
msum = max + min;
ssn = sum - msum;
System.out.println("2nd smallest number:"+ssn);
}
}
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;
class P8
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a,b,c;
System.out.println("Enter three numbers");
a=in.nextInt();
b=in.nextInt();
c=in.nextInt();
if(a!=b && b!=c && c!=a)
{
if( a > b && a > c)
System.out.println("Greatest number:"+a);
else if (b > a && b > c)
System.out.println("Greatest number:"+b);
else
System.out.println("Greatest number:"+c);
if( a < b && a < c)
System.out.println("Smallest number:"+a);
else if (b < a && b < c)
System.out.println("Smallest number:"+b);
else
System.out.println("Smallest number:"+c);
}
else
System.out.println("All numbers are equal");
}
}
Question 9

A Pre-Paid taxi charges from the passenger as per the tariff given below:

Distance Rate

Up to 5 km ₹ 100

For the next 10 km ₹ 10/km

For the next 10 km ₹ 8/km

More than 25 km ₹ 5/km

Write a program to input the distance covered and calculate the amount paid by the passenger. The program displays the printed bill with the details
given below:
Taxi No. :
Distance covered :
Amount :

import java.util.*;
class P9
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int dc,amt=0;
String tno;
System.out.println("Enter Taxi No");
tno=in.nextLine();
System.out.println("Enter distance covered");
dc=in.nextInt();
if(dc<=5)
amt=100;
if(dc>5 && dc<=15)
amt=100+(dc-5)*10;
if(dc>15 && dc<=25)
amt=100+100+(dc-15)*8;
if(dc>25)
amt=100+100+80+(dc-25)*5;
System.out.println("Taxi No. : "+tno);
System.out.println("Distance covered : "+dc);
System.out.println("Amount :Rs."+amt);

}
}
Question 10

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 ₹ 2,000 5% Calculator

₹ 2,001 to ₹ 5,000 10% School Bag


Total Cost Discount Gift

₹ 5,001 to ₹ 10,000 15% Wall Clock

Above ₹ 10,000 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.

import java.util.*;
class P10
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int tc;
double amt=0.0,d;
System.out.println("Enter total cost of purchase");
tc=in.nextInt();
if(tc<=2000)
{
d=(tc*5)/100;
amt=tc-d;
System.out.println("Amount to be paid:Rs."+amt);
System.out.println("Gift : Calculator");
}
if(tc>=2001 && tc<=5000)
{
d=(tc*10)/100;
amt=tc-d;
System.out.println("Amount to be paid:Rs."+amt);
System.out.println("Gift : School Bag");
}
if(tc>=5001 && tc<=10000)
{
d=(tc*15)/100;
amt=tc-d;
System.out.println("Amount to be paid:Rs."+amt);
System.out.println("Gift : Wall Clock");
}
if(tc>10000)
{
d=(tc*20)/100;
amt=tc-d;
System.out.println("Amount to be paid:Rs."+amt);
System.out.println("Gift : Wrist Watch");
}
}
}
Question 11

Given below is a hypothetical table showing rate of income tax for an India citizen, who is below or up to 60 years.

Taxable income (TI) in ₹ Income Tax in ₹

Up to ₹ 2,50,000 Nil

More than ₹ 2,50,000 and less than or equal to ₹ 5,00,000 (TI - 1,60,000) * 10%

More than ₹ 5,00,000 and less than or equal to ₹ 10,00,000 (TI - 5,00,000) * 20% + 34,000

More than ₹ 10,00,000 (TI - 10,00,000) * 30% + 94,000

Write a program to input the name, age and taxable income of a person. If the age is more than 60 years then display the message "Wrong
Category". If the age is less than or equal to 60 years then compute and display the income tax payable along with the name of tax payer, as per the
table given above.

import java.util.*;
class P11
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int TI,age;
String name;
double tax=0.0;
System.out.println("Enter age");
age=in.nextInt();
if(age<=60)
{
System.out.println("Enter Name");
name=in.nextLine();
System.out.println("Enter Taxable income");
TI=in.nextInt();
if(TI<=250000)
tax=(TI*0.0)/100;
if(TI>250000 && TI<=500000)
tax=0+((TI-250000)*10.0)/100.0;
if(TI>500000 && TI<=1000000)
tax=0+25000+((TI-500000)*20.0)/100.0;
if(TI>1000000)
tax=0+25000+100000+((TI-1000000)*30.0)/100.0;
System.out.println("Name of the tax payer :"+name);
System.out.println("Tax to be paid:"+tax);
}
else
System.out.println("Wrong category");
}
}
Question 12

An employee wants to deposit certain sum of money under 'Term Deposit' scheme in Syndicate Bank. The bank has provided the tariff of the
scheme, which is given below:

No. of Days Rate of Interest

Up to 180 days 5.5%

181 to 364 days 7.5%

Exact 365 days 9.0%

More than 365 days 8.5%

Write a program to calculate the maturity amount taking the sum and number of days as inputs.

import java.util.*;
class P12
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int d,amt;
double mamt=0.0,inter=0.0;
System.out.println("Enter Sum to be deposited in bank");
amt=in.nextInt();
System.out.println("Enter no of days");
d=in.nextInt();
if(d<=180)
inter=(amt*5.5)/100;
if(d>180 && d<=364)
inter=(amt*7.5)/100;
if(d==365)
inter=(amt*9.0)/100;
if(d>365)
inter=(amt*8.5)/100;

mamt=amt+inter;

System.out.println("Sum deposited in bank: "+amt);


System.out.println("Maturity amount: Rs."+mamt);
}
}
Question 13

Mr. Kumar is an LIC agent. He offers discount to his policy holders on the annual premium. However, he also gets commission on the sum assured
as per the given tariff.

Sum Assured Discount Commission

Up to ₹ 1,00,000 5% 2%

₹ 1,00,001 and up to ₹ 2,00,000 8% 3%

₹ 2,00,001 and up to ₹ 5,00,000 10% 5%

More than ₹ 5,00,000 15% 7.5%

Write a program to input name of the policy holder, the sum assured and first annual premium. Calculate the discount of the policy holder and the
commission of the agent. The program displays all the details as:
Name of the policy holder :
Sum assured :
Premium :
Discount on the first premium :
Commission of the agent :

import java.util.*;
class P13
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
double ap,sa,dp=0.0,ca=0.0;
String name;
System.out.println("Name of the policy holder");
name=in.nextLine();
System.out.println("Enter sum assured");
sa=in.nextDouble();
System.out.println("Enter Final Annual Premium");
ap=in.nextDouble();
if(sa<=100000)
{
dp=(ap*5.0)/100;
ca=(sa*2.0)/100;
}
if(sa>=100001 && sa<=200000)
{
dp=(ap*8.0)/100;
ca=(sa*3.0)/100;
}
if(sa>=200001 && sa<=500000)
{
dp=(ap*10.0)/100;
ca=(sa*5.0)/100;
}
if(sa>500000)
{
dp=(ap*15.0)/100;
ca=(sa*7.5)/100;
}
System.out.println("Name of the policy holder: "+name);
System.out.println("Sum assured: Rs."+sa);
System.out.println("Premium: Rs."+ap);
System.out.println("Discount on first premium: Rs."+dp);
System.out.println("Commission of agent: Rs."+ca);
}
}
Question 14

A company announces revised Dearness Allowance (DA) and Special Allowances (SA) for their employees as per the tariff given below:

Basic Dearness Allowance (DA) Special Allowance (SA)

Up to ₹ 10,000 10% 5%

₹ 10,001 - ₹ 20,000 12% 8%

₹ 20,001 - ₹ 30,000 15% 10%

₹ 30,001 and above 20% 12%

Write a program to accept name and Basic Salary (BS) of an employee. Calculate and display gross salary.
Gross Salary = Basic + Dearness Allowance + Special Allowance
Print the information in the given format:
Name Basic DA Spl. Allowance Gross Salary
xxx xxx xxx xxx xxx

import java.util.*;
class P14
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int sal;
double da=0.0,sa=0.0,gsal=0.0;
String name;
System.out.println("Name of the employee");
name=in.nextLine();
System.out.println("Basic salary");
sal=in.nextInt();

if(sal<=10000)
{
da=(sal*10.0)/100.0;
sa=(sal*5.0)/100.0;
}
if(sal>=10001 && sal<=20000)
{
da=(sal*12.0)/100.0;
sa=(sal*8.0)/100.0;
}
if(sal>=20001 && sal<=30000)
{
da=(sal*15.0)/100.0;
sa=(sal*10.0)/100.0;
}
if(sal>30000)
{
da=(sal*20.0)/100.0;
sa=(sal*12.0)/100.0;
}
gsal=sal+da+sa;
System.out.println("Name \t Basic \t DA \t Spl.Allowance \t Gross Salry");
System.out.println(name+"\t"+sal+"\t"+da+"\t"+sa+"\t"+gsal);
}
}
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.*;
class P15
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int f,c,ch;
double F=0.0,C=0.0;
System.out.println("Enter your choice as 1 for temp from Fahrenheit to
Celcius and 2 for Celcius to Fahrenheit");
ch=in.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter temperature in Fahrenheit");
f=in.nextInt();
C=(f-32)*5/9;
System.out.println("Entered Temperature in Fahrenheit:"+f);
System.out.println("Temperature in Celcius:"+C);
break;
case 2:
System.out.println("Enter temperature in Celcius");
c=in.nextInt();
F=1.8*c+32;
System.out.println("Entered Temperature in Celcius:"+c);
System.out.println("Temperature in Fahrenheit:"+F);
break;
default:
System.out.println("Wrong choice");
}
}
}
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.*;
class P16
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int ch,l,b,h,r;
double v;
System.out.println("Enter your choice as 1 for volume of cuboid ,2 for
cylinder, 3 for cone");
ch=in.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter length");
l=in.nextInt();
System.out.println("Enter breadth");
b=in.nextInt();
System.out.println("Enter height");
h=in.nextInt();
v=l*b*h;
System.out.println("Vol of cuboid: "+v);
break;
case 2:
System.out.println("Enter radius");
r=in.nextInt();
System.out.println("Enter height");
h=in.nextInt();
v=(22/7)*r*r*h;
System.out.println("Vol of cylinder: "+v);
break;
case 3:
System.out.println("Enter radius");
r=in.nextInt();
System.out.println("Enter height");
h=in.nextInt();
v=(1/3)*(22/7)*r*r*h; //(1*22*r*r*h)/(3*7);
System.out.println("Vol of cone: "+v);
break;
default:
System.out.println("Wrong choice");
}
}
}
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.*;
class P17
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int ch,tamt=0;
String name;
System.out.println("Enter your choice as 1 for Ground,2 for First, 3 for
Second floor and 4 for Third Floor");
ch=in.nextInt();
in.nextLine();
switch(ch)
{
case 1:
System.out.println("Kids Wears");
System.out.println("Enter Name of the shop");
name=in.nextLine();
System.out.println("Enter shopping amount.");
tamt=in.nextInt();
System.out.println("Name of the shop: "+name);
System.out.println("Total amount:Rs."+tamt);
System.out.println("Visit Again!!!");
break;
case 2:
System.out.println("Ladies Wears");
System.out.println("Enter Name of the shop");
name=in.nextLine();
System.out.println("Enter shopping amount.");
tamt=in.nextInt();
System.out.println("Name of the shop: "+name);
System.out.println("Total amount:Rs."+tamt);
System.out.println("Visit Again!!!");
break;
case 3:
System.out.println("Designer Sarees");
System.out.println("Enter shopping amount:Rs.");
tamt=in.nextInt();
in.nextLine();
System.out.println("Enter Name of the shop");
name=in.nextLine();
System.out.println("Name of the shop: "+name);
System.out.println("Total amount:Rs."+tamt);
System.out.println("Visit Again!!!");
break;
case 4:
System.out.println("Men's Wear");
System.out.println("Enter shopping amount:Rs.");
tamt=in.nextInt();
in.nextLine();
System.out.println("Enter Name of the shop");
name=in.nextLine();
System.out.println("Name of the shop: "+name);
System.out.println("Total amount:Rs."+tamt);
System.out.println("Visit Again!!!");
break;
default:
System.out.println("Wrong choice");
}
}
}
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.*;
class P18
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int ch,r1,r2,R1=0;
double R2=0.0;
System.out.println("Enter your choice as 1 for equivalent resistance in
series ,2 for parallel");
ch=in.nextInt();
System.out.println("Enter value of resistance 1");
r1=in.nextInt();
System.out.println("Enter value of resistance 2");
r2=in.nextInt();
switch(ch)
{
case 1:
R1=r1+r2;
System.out.println("Equivalent resistance in series: "+R1);
break;
case 2:
R2=(r1*r2)/(r1+r2);
System.out.println("Equivalent resistance in parallel: "+R2);
break;
default:
System.out.println("Wrong choice");
}
}
}
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.*;
class P19
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int p,r,t,P,R,T;
char ch;
double SI=0.0,CI=0.0;
System.out.println("Enter 'S' for Simple Interest and 'C' for Compund
Interest");
ch=in.next().charAt(0);
switch(ch)
{
case 'S':
System.out.println("Enter Sum");
p=in.nextInt();
System.out.println("Enter Rate");
r=in.nextInt();
System.out.println("Enter Time");
t=in.nextInt();
SI=(p*r*t)/100.0;
System.out.println("Sum :Rs."+p);
System.out.println("Simple Interest earned:Rs."+SI);
break;
case 'C':
double R2;
System.out.println("Enter Sum");
P=in.nextInt();
System.out.println("Enter Rate");
R=in.nextInt();
System.out.println("Enter Time");
T=in.nextInt();
CI=P*(Math.pow((1+(R/100.0)),T)-1);
System.out.println("Sum :Rs."+P);
System.out.println("Compound Interest earned:Rs."+CI);
break;
default:
System.out.println("Wrong choice");
}
}
}
Question 20

'Kumar Electronics' has announced the following seasonal discounts on purchase of certain items.

Purchase Amount Discount on Laptop Discount on Desktop PC

Up to ₹ 25000 0.0% 5.0%

₹ 25,001 to ₹ 50,000 5% 7.5%

₹ 50,001 to ₹ 1,00,000 7.5% 10.0%

More than ₹ 1,00,000 10.0% 15.0%

Write a program to input name, amount of purchase and the type of purchase (`L' for Laptop and 'D' for Desktop) by a customer. Compute and print
the net amount to be paid by a customer along with his name.
(Net amount = Amount of purchase - discount)
import java.util.*;
class P20
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int p=0;
char ch;
double namt=0.0,d=0.0;
String name="";
System.out.println("Enter your choice as 'L' for Laptop and 'D' for Desktop
computer");
ch=in.next().charAt(0);
in.nextLine(); //Consume newline left-over
switch(ch)
{
case 'L':
System.out.println("Enter name");
name=in.nextLine();
System.out.println("Enter amount of purchase");
p=in.nextInt();
if(p<=25000)
{
d=(p*0.0)/100;
namt=p-d;
}
if(p>25000 && p<=50000)
{
d=(p*5.0)/100;
namt=p-d;
}
if(p>50000 && p<=100000)
{
d=(p*7.5)/100;
namt=p-d;
}
if(p>100000)
{
d=(p*10.0)/100;
namt=p-d;
}
System.out.println("Name :"+name);
System.out.println("Net amount to be paid after discount:Rs."+namt);
break;
case 'D':
System.out.println("Enter name");
name=in.nextLine();
System.out.println("Enter amount of purchase");
p=in.nextInt();
if(p<=25000)
{
d=(p*5.0)/100;
namt=p-d;
}
if(p>25000 && p<=50000)
{
d=(p*7.5)/100;
namt=p-d;
}
if(p>50000 && p<=100000)
{
d=(p*10.0)/100;
namt=p-d;
}
if(p>100000)
{
d=(p*15.0)/100;
namt=p-d;
}
System.out.println("Name :"+name);
System.out.println("Net amount to be paid after discount:Rs."+namt);
break;
default:
System.out.println("Wrong choice");
}
}
}

You might also like