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 java.util.Scanner;
public class SimplePendulum {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Input length and acceleration due to gravity
System.out.print("Enter the length of the pendulum (in meters): ");
double l = sc.nextDouble();
System.out.print("Enter the acceleration due to gravity (in m/s²): ");
double g = sc.nextDouble();
// Calculate the time period
double T = 2 * Math.PI * Math.sqrt(l / g);
// Display the result
System.out.printf("The time period of the simple pendulum is: %.2f
seconds\n", T);
sc.close();
}
}
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 java.util.Scanner;
public class EmployeePay {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Input
System.out.print("Enter Basic Pay of the employee: ");
double basicPay = sc.nextDouble();
// 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
System.out.println("Dearness Allowance (DA): " + da);
System.out.println("House Rent Allowance (HRA): " + hra);
System.out.println("Provident Fund (PF): " + pf);
System.out.println("Gross Pay: " + grossPay);
System.out.println("Net Pay: " + netPay);
sc.close();
}
}
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;
public class CameraPurchase {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Input: Printed price
System.out.print("Enter the printed price of the digital camera: ");
double printedPrice = sc.nextDouble();
// 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
System.out.println("Discount: " + discount);
System.out.println("Price after discount: " + priceAfterDiscount);
System.out.println("GST (6%): " + gst);
System.out.println("Final amount to be paid: " + finalAmount);
sc.close();
}
}
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;
public class DiscountComparison {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Input: Original price of the article
System.out.print("Enter the price of the article: ");
double price = sc.nextDouble();
// 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
System.out.println("\n--- Shopkeeper A ---");
System.out.println("Single 30% Discount: " + discountA);
System.out.println("Final Price: " + finalPriceA);
System.out.println("\n--- Shopkeeper B ---");
System.out.println("First Discount (20%): " + firstDiscount);
System.out.println("Second Discount(10% on reduced price):"+
secondDiscount);
System.out.println("Total Discount: " + totalDiscountB);
System.out.println("Final Price: " + finalPriceB);
// Comparison
System.out.println("\n--- Comparison ---");
if (finalPriceA < finalPriceB) {
System.out.println("Shopkeeper A offers a better deal.");
} else if (finalPriceB < finalPriceA) {
System.out.println("Shopkeeper B offers a better deal.");
} else {
System.out.println("Both shopkeepers offer the same final price.");
}
sc.close();
}
}
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 java.util.Scanner;
public class CompoundInterestBreakup {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Input: Principal amount
System.out.print("Enter the principal amount: ");
double principal = sc.nextDouble();
double rate = 5.0;
// Calculate amount at the end of each year
double amount1 = principal * Math.pow(1 + rate / 100, 1);
double interest1 = amount1 - principal;
double amount2 = principal * Math.pow(1 + rate / 100, 2);
double interest2 = amount2 - amount1;
double amount3 = principal * Math.pow(1 + rate / 100, 3);
double interest3 = amount3 - amount2;
// Output
System.out.printf("\nInterest for the 1st year: ₹%.2f\n", interest1);
System.out.printf("Interest for the 2nd year: ₹%.2f\n", interest2);
System.out.printf("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 java.util.Scanner;
public class ShareCalculator {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Constants
double nominalValue = 10.0;
double dividendPercent = 10.0;
int targetShares = 3000;
// Input: Annual dividend received
System.out.print("Enter the annual dividend received: ₹");
double annualDividend = sc.nextDouble();
// Calculate number of shares already held
int sharesHeld = (int)((annualDividend * 100) / (nominalValue *
dividendPercent));
// Calculate additional shares needed
int sharesToBuy = targetShares - sharesHeld;
// Output
System.out.println("Number of shares already held: " + sharesHeld);
System.out.println("Number of shares to be purchased to reach target: " +
sharesToBuy);
sc.close();
}
}
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 java.util.Scanner;
public class TimeConverter {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Input: time in seconds
System.out.print("Enter time in seconds: ");
int totalSeconds = sc.nextInt();
// Conversion
int hours = totalSeconds / 3600;
int remainingSeconds = totalSeconds % 3600;
int minutes = remainingSeconds / 60;
int seconds = remainingSeconds % 60;
// Output
System.out.println(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 java.util.Scanner;
public class SwapWithoutThird {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Input two unequal numbers
System.out.print("Enter the first number (a): ");
int a = sc.nextInt();
System.out.print("Enter the second number (b): ");
int b = sc.nextInt();
// 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
System.out.println("After swapping:");
System.out.println("a = " + a);
System.out.println("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 java.util.Scanner;
public class InterestDifference {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Input: Principal amount
System.out.print("Enter the principal amount: ");
double P = sc.nextDouble();
// 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 * Math.pow(1 + R / 100, T); // amount after compound interest
double CI = A - P;
// Calculate difference
double difference = CI - SI;
// Output
System.out.printf("Simple Interest (SI): "+ SI);
System.out.printf("Compound Interest (CI): "+ CI);
System.out.printf("Difference (CI - SI): "+ difference);
sc.close();
}
}
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 java.util.Scanner;
public class CalculatorCostPrice {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Input: Selling price of each calculator
System.out.print("Enter the selling price of each calculator: ");
double SP = sc.nextDouble();
// 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
System.out.printf("Cost Price of first calculator (20%% profit): "+CP1);
System.out.printf("Cost Price of second calculator (20%% loss): ",+CP2);
System.out.printf("Total Cost Price of both calculators:”+ totalCP);
sc.close();
}
}