Class IX – INPUT IN JAVA – PROGRAM LIST
1. The time period of a Simple Pendulum is given by the formula:
T = 2π√(l/g)
Write a program to calculate the time period of a Simple Pendulum by taking
length(l) and acceleration due to gravity (g) as inputs.
import [Link];
public class SimplePendulum {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
// Input length and acceleration due to gravity
[Link]("Enter the length of the pendulum (in meters): ");
double l = [Link]();
[Link]("Enter the acceleration due to gravity (in m/s²): ");
double g = [Link]();
// Calculate the time period
double T = 2 * [Link] * [Link](l / g);
// Display the result
[Link]("The time period of the simple pendulum is: %.2f
seconds\n", T);
[Link]();
}
}
2. Write a program by using class 'Employee' to accept Basic Pay of an employee.
Calculate the allowances/deductions as given below.
Allowance / Deduction Rate
Dearness Allowance (DA) 30% of Basic Pay
House Rent Allowance (HRA) 15% of Basic Pay
Provident Fund (PF) 12.5% of Basic Pay
Finally, find and print the Gross and Net pay.
Gross Pay = Basic Pay + Dearness Allowance + House Rent Allowance
Net Pay = Gross Pay - Provident Fund
import [Link];
public class EmployeePay {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
// Input
[Link]("Enter Basic Pay of the employee: ");
double basicPay = [Link]();
// Calculations
double da = 0.30 * basicPay;
double hra = 0.15 * basicPay;
double pf = 0.125 * basicPay;
double grossPay = basicPay + da + hra;
double netPay = grossPay - pf;
// Output
[Link]("Dearness Allowance (DA): " + da);
[Link]("House Rent Allowance (HRA): " + hra);
[Link]("Provident Fund (PF): " + pf);
[Link]("Gross Pay: " + grossPay);
[Link]("Net Pay: " + netPay);
[Link]();
}
}
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 [Link];
public class CameraPurchase {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
// Input: Printed price
[Link]("Enter the printed price of the digital camera: ");
double printedPrice = [Link]();
// Step 1: Calculate 10% discount
double discount = 0.10 * printedPrice;
double priceAfterDiscount = printedPrice - discount;
// Step 2: Add 6% GST on discounted price
double gst = 0.06 * priceAfterDiscount;
double finalAmount = priceAfterDiscount + gst;
// Output
[Link]("Discount: " + discount);
[Link]("Price after discount: " + priceAfterDiscount);
[Link]("GST (6%): " + gst);
[Link]("Final amount to be paid: " + finalAmount);
[Link]();
}
}
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 [Link];
public class DiscountComparison {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
// Input: Original price of the article
[Link]("Enter the price of the article: ");
double price = [Link]();
// Shopkeeper A: 30% discount
double discountA = 0.30 * price;
double finalPriceA = price - discountA;
// Shopkeeper B: two successive discounts - 20% then 10%
double firstDiscount = 0.20 * price;
double priceAfterFirst = price - firstDiscount;
double secondDiscount = 0.10 * priceAfterFirst;
double finalPriceB = priceAfterFirst - secondDiscount;
double totalDiscountB = price - finalPriceB;
// Output
[Link]("\n--- Shopkeeper A ---");
[Link]("Single 30% Discount: " + discountA);
[Link]("Final Price: " + finalPriceA);
[Link]("\n--- Shopkeeper B ---");
[Link]("First Discount (20%): " + firstDiscount);
[Link]("Second Discount(10% on reduced price):"+
secondDiscount);
[Link]("Total Discount: " + totalDiscountB);
[Link]("Final Price: " + finalPriceB);
// Comparison
[Link]("\n--- Comparison ---");
if (finalPriceA < finalPriceB) {
[Link]("Shopkeeper A offers a better deal.");
} else if (finalPriceB < finalPriceA) {
[Link]("Shopkeeper B offers a better deal.");
} else {
[Link]("Both shopkeepers offer the same final price.");
}
[Link]();
}
}
5. Mr. Agarwal invests certain sum at 5% per annum compound interest for three
years. Write a program in Java to calculate:
(a) the interest for the first year
(b) the interest for the second year
(c) the interest for the third year.
Take sum as an input from the user.
import [Link];
public class CompoundInterestBreakup {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
// Input: Principal amount
[Link]("Enter the principal amount: ");
double principal = [Link]();
double rate = 5.0;
// Calculate amount at the end of each year
double amount1 = principal * [Link](1 + rate / 100, 1);
double interest1 = amount1 - principal;
double amount2 = principal * [Link](1 + rate / 100, 2);
double interest2 = amount2 - amount1;
double amount3 = principal * [Link](1 + rate / 100, 3);
double interest3 = amount3 - amount2;
// Output
[Link]("\nInterest for the 1st year: ₹%.2f\n", interest1);
[Link]("Interest for the 2nd year: ₹%.2f\n", interest2);
[Link]("Interest for the 3rd year: ₹%.2f\n", interest3);
}
}
6. A businessman wishes to accumulate 3000 shares of a company. However, he
already has some shares of that company valuing ₹10 (nominal value) which yield
10% dividend per annum and receive ₹2000 as dividend at the end of the year. Write
a program in Java to calculate and display the number of shares he has and to
calculate how many more shares to be purchased to make his target.
Hint: No. of share = (Annual dividend * 100) / (Nominal value * div%)
import [Link];
public class ShareCalculator {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
// Constants
double nominalValue = 10.0;
double dividendPercent = 10.0;
int targetShares = 3000;
// Input: Annual dividend received
[Link]("Enter the annual dividend received: ₹");
double annualDividend = [Link]();
// Calculate number of shares already held
int sharesHeld = (int)((annualDividend * 100) / (nominalValue *
dividendPercent));
// Calculate additional shares needed
int sharesToBuy = targetShares - sharesHeld;
// Output
[Link]("Number of shares already held: " + sharesHeld);
[Link]("Number of shares to be purchased to reach target: " +
sharesToBuy);
[Link]();
}
}
7. Write a program to input time in seconds. Display the time after converting
them into hours, minutes and seconds.
Sample Input: Time in seconds: 5420
Sample Output: 1 Hour 30 Minutes 20 Seconds
import [Link];
public class TimeConverter {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
// Input: time in seconds
[Link]("Enter time in seconds: ");
int totalSeconds = [Link]();
// Conversion
int hours = totalSeconds / 3600;
int remainingSeconds = totalSeconds % 3600;
int minutes = remainingSeconds / 60;
int seconds = remainingSeconds % 60;
// Output
[Link](hours+" Hour"+minutes+" Minutes"+seconds+" Seconds");
}
}
8. Write a program to input two unequal numbers. Display the numbers after
swapping their values in the variables without using a third variable.
Sample Input: a = 23, b = 56
Sample Output: a = 56, b = 23
import [Link];
public class SwapWithoutThird {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
// Input two unequal numbers
[Link]("Enter the first number (a): ");
int a = [Link]();
[Link]("Enter the second number (b): ");
int b = [Link]();
// Swap without third variable
a = a + b; // sum of both numbers
b = a - b; // b becomes original value of a
a = a - b; // a becomes original value of b
// Output
[Link]("After swapping:");
[Link]("a = " + a);
[Link]("b = " + b);
}
}
9. A certain sum is invested at the rate of 10% per annum for 3 years. Write a
program find and display the difference between Compound Interest (CI) and Simple
Interest (SI). Take the sum as an input.
Hint: A = P * (1 + (R/100))T , SI = (P*T*R)/100 and CI = A – P
import [Link];
public class InterestDifference {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
// Input: Principal amount
[Link]("Enter the principal amount: ");
double P = [Link]();
// Constants
double R = 10.0; // rate of interest
int T = 3; // time in years
// Calculate Simple Interest
double SI = (P * R * T) / 100;
// Calculate Compound Interest
double A = P * [Link](1 + R / 100, T); // amount after compound interest
double CI = A - P;
// Calculate difference
double difference = CI - SI;
// Output
[Link]("Simple Interest (SI): "+ SI);
[Link]("Compound Interest (CI): "+ CI);
[Link]("Difference (CI - SI): "+ difference);
[Link]();
}
}
10.A Shopkeeper sells two calculators for the same price. He earns 20% profit on
one and suffers a loss of 20% on the other. Write a program to find and display
his total cost price of the calculators by taking their selling price as input.
Hint: CP=(SP/(1+ profit/100)) when profit,
2CP = (SP/(1-loss/100)) when loss.
import [Link];
public class CalculatorCostPrice {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
// Input: Selling price of each calculator
[Link]("Enter the selling price of each calculator: ");
double SP = [Link]();
// First calculator: 20% profit
double CP1 = SP / (1 + 20.0 / 100);
// Second calculator: 20% loss
double CP2 = SP / (1 - 20.0 / 100);
// Total cost price
double totalCP = CP1 + CP2;
// Output
[Link]("Cost Price of first calculator (20%% profit): "+CP1);
[Link]("Cost Price of second calculator (20%% loss): ",+CP2);
[Link]("Total Cost Price of both calculators:”+ totalCP);
[Link]();
}
}