Professional Documents
Culture Documents
Question 1
Write a program to calculate the time period of a Simple Pendulum by taking length and acceleration due to gravity (g) as inputs.
The time period of a Simple Pendulum is given by the formula:
T = 2π√(l/g)
import java.util.Scanner;
Output
Question 2
Write a program by using class 'Employee' to accept Basic Pay of an employee. Calculate the allowances/deductions as given below.
import java.util.Scanner;
public class Employee
{
public void computePay() {
Scanner in = new Scanner(System.in);
System.out.print("Enter Basic Pay: ");
double bp = in.nextDouble();
double da = 0.3 * bp;
double hra = 0.15 * bp;
double pf = 0.125 * bp;
double gp = bp + da + hra;
double np = gp - pf;
System.out.println("Gross Pay = " + gp);
System.out.println("Net Pay = " + np);
}
}
Output
Question 3
A shopkeeper offers 10% discount on the printed price of a Digital Camera. However, a customer has to pay 6% GST on the remaining amount.
Write a program in Java to calculate the amount to be paid by the customer taking printed price as an input.
import java.util.Scanner;
Output
Question 4
A shopkeeper offers 30% discount on purchasing articles whereas the other shopkeeper offers two successive discounts 20% and 10% for
purchasing the same articles. Write a program in Java to compute and display the discounts.
Take the price of an article as the input.
import java.util.Scanner;
Output
Question 5
Mr. Agarwal invests certain sum at 5% per annum compound interest for three years. Write a program in Java to calculate:
import java.util.Scanner;
Output
Topic :- Mathematical Library Methods
Question 1
Write a program in Java to input three numbers and display the greatest and the smallest of the two numbers.
Hint: Use Math.min( ) and Math.max( )
Sample Input: 87, 65, 34
Sample Output: Greatest Number 87
Smallest number 34
import java.util.Scanner;
Output
Question 2
Write a program in Java to calculate and display the hypotenuse of a Right-Angled Triangle by taking perpendicular and base as inputs.
2 2
Hint: h = √p + b
import java.util.Scanner;
Output
Question 3
Write a program to input a number and evaluate the results based on the number entered by the user:
(a) Natural logarithm of the number
(b) Absolute value of the number
(c) Square root of the number
(d) Cube of the number
(e) Random numbers between 0 (zero) and 1 (one).
import java.util.Scanner;
Output
Question 4
In an examination, you have appeared for three subjects i.e. Physics, Chemistry and Biology. Write a program in Java to calculate the average
mark obtained and finally display the marks in rounded-off form.
Take Physics, Chemistry. and Biology marks as inputs.
import java.util.Scanner;
Output
Question 5
import java.util.Scanner;
Output
Topic :- Conditional Constructs in Java (
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;
Output
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;
Output
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;
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;
Output
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;
Output
Question 5
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;
}
}
Output
Question 6
A Pre-Paid taxi charges from the passenger as per the tariff given below:
Distance Rate
Up to 5 km ₹ 100
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.Scanner;
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;
}
}
Output
Question 7
A cloth showroom has announced festival discounts and the gifts on the purchase of items, based on the total cost as given below:
Up to ₹ 2,000 5% Calculator
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.Scanner;
}
}
Output
Question 8
Given below is a hypothetical table showing rate of income tax for an India citizen, who is below or up to 60 years.
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
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.Scanner;
Output
Question 9
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.
Up to ₹ 1,00,000 5% 2%
import java.util.Scanner;
}
}
Output
Question 10
A company announces revised Dearness Allowance (DA) and Special Allowances (SA) for their employees as per the tariff given below:
Up to ₹ 10,000 10% 5%
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.Scanner;
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);
}
}
Output
Question 11
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;
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;
}
}
}
Output
Question 12
The volume of solids, viz. cuboid, cylinder and cone can be calculated by the formula:
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;
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.");
}
}
}
Output
Question 13
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:
Write a program to perform the above task as per the user's choice.
import java.util.Scanner;
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:
isFloorValid = false;
System.out.println("Incorrect Floor");
break;
}
if (isFloorValid) {
System.out.print("Enter bill amount: ");
double amt = in.nextDouble();
Output
Question 14
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;
switch (choice) {
case 1:
eqr = r1 + r2;
break;
case 2:
eqr = (r1 * r2) / (r1 + r2);
break;
default:
isChoiceValid = false;
System.out.println("Incorrect choice");
break;
}
if (isChoiceValid)
System.out.println("Equivalent resistance = " + eqr);
}
}
Output
Question 15
The Simple Interest (SI) and Compound Interest (CI) of a sum (P) for a given time (T) and rate (R) can be calculated as:
T
(a) SI = (p * r * t) / 100 (b) CI = P * ((1 + (R / 100)) - 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;
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);
}
}
}
Output
Question 16
'Kumar Electronics' has announced the following seasonal discounts on purchase of certain items.
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.Scanner;
Output
Topic :- Iterative Constructs in Java
Question 11
The Greatest Common Divisor (GCD) of two integers is calculated by the continued division method. Divide the larger number by the smaller,
the remainder then divides the previous divisor. The process repeats unless the remainder reaches to zero. The last divisor results in GCD.
Sample Input: 45, 20
Sample Output: GCD=5
import java.util.Scanner;
Output
Question 2
import java.util.Scanner;
Output
Question 3
Write a program to calculate the sum of all odd numbers and even numbers between a range of numbers from m to n (both inclusive) where
m < n. Input m and n (where m<n).
import java.util.Scanner;
if (m > n) {
System.out.println("m should be less than n");
}
else {
for (int i = m; i <=n; i++) {
if (i % 2 == 0)
sumEven += i;
else
sumOdd += i;
}
Output
Question 4
Write a program to enter any 50 numbers and check whether they are divisible by 5 or not. If divisible then perform the following tasks:
(a) Display all the numbers ending with the digit 5.
(b) Count those numbers ending with 0 (zero).
import java.util.Scanner;
Output
Question 5
Write a program to display all the numbers between m and n input from the keyboard (where m<n, m>0, n>0), check and print the numbers
that are perfect square. e.g. 25, 36, 49, are said to be perfect square numbers.
import java.util.Scanner;
Output
Question 6
Write a program to display all the 'Buzz Numbers' between p and q (where p<q). A 'Buzz Number' is the number which ends with 7 or is
divisible by 7.
import java.util.Scanner;
}
}
Output
Question 7
Write a program to input marks in English, Maths and Science of 40 students who have passed ICSE Examination 2014. Now, perform the
following tasks:
(a) Number of students, who have secured 95% or more in all the subjects.
(b) Number of students, who have secured 90% or more in English, Maths and Science.
import java.util.Scanner;
Output
Question 8
Write a program in Java to find the sum of the given series :
(a) 1 + 4 + 9 + ...... + 400
Output
Output
(c) 1 + (1/3) + (1/5) + ...... + (1/19)
Output
(e) 2 - 4 + 6 - 8 + ...... - 20
Output
Output
Question 9
Write a program to input a number and count the number of digits. The program further checks whether the number contains odd number of
digits or even number of digits.
Sample Input: 749
Sample Output: Number of digits=3
The number contains odd number of digits.
import java.util.Scanner;
while (n != 0) {
dc++;
n /= 10;
}
if (dc % 2 == 0)
System.out.println("The number contains even number of digits");
else
System.out.println("The number contains odd number of digits");
}
}
Output
Question 10
Write a program to input a number and display the new number after reversing the digits of the original number. The program also displays
the absolute difference between the original number and the reversed number.
Sample Input: 194
Sample Output: 491
Absolute Difference= 297
import java.util.Scanner;
while(copyNum != 0) {
int digit = copyNum % 10;
copyNum /= 10;
revNum = revNum * 10 + digit;
}
Output
Topic :- Nested For Loops
Question 1
1
2 1
3 21
4 321
5 4321
Output
Question 2
1 2345
1 234
1 23
1 2
1
Output
Question 3
5 4321
5 432
5 43
5 4
5
Output
Question 4
1 3579
1 357
1 35
1 3
1
Output
Question 5
5
5 4
5 43
5 432
5 4321
Output
Question 6
1 2345
2 345
3 45
4 5
5
Output
Question 7
9 9 9 9 9
7 7 7 7 7
5 5 5 5 5
3 3 3 3 3
1 1 1 1 1
Output
Question 8
9
7 9
5 79
3 579
1 3579
Output
Question 9
9
9 7
9 75
9 753
9 7531
Output
Question 10
1
23
456
7 8 9 10
11 12 13 14 15
Output