0% found this document useful (0 votes)
32 views7 pages

Java Programs for Class 9 ICSE

The document contains a list of Java programs for various calculations, including the time period of a simple pendulum, employee pay calculations, discounts on products, compound interest breakdown, and more. Each program includes user input, calculations, and output display. The programs cover a range of topics suitable for Class IX students learning Java programming.

Uploaded by

nanditha629
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views7 pages

Java Programs for Class 9 ICSE

The document contains a list of Java programs for various calculations, including the time period of a simple pendulum, employee pay calculations, discounts on products, compound interest breakdown, and more. Each program includes user input, calculations, and output display. The programs cover a range of topics suitable for Class IX students learning Java programming.

Uploaded by

nanditha629
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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]();
}
}

Common questions

Powered by AI

Calculate Simple Interest (SI) using the formula SI = (P * R * T) / 100. Calculate Compound Interest (CI) for three years using A = P * Math.pow(1 + R/100, T). The difference between CI and SI indicates the effectiveness of compounding over simple interest. Use these computational methods to reveal the more beneficial interest type over a defined period .

To compute compound interest over three years, calculate the amount at the end of each year using A = P * Math.pow(1 + rate/100, t), where t is the year number. The interest each year is the difference between subsequent amounts. Start with the principal for the first year to compute the first year's amount, subtract principal to get the year's interest, then recursively calculate following years using previous amounts. This program details through iterative formula application .

The final amount paid by the customer is calculated by first applying a 10% discount on the printed price of the camera, then applying a 6% GST on the discounted price. In Java, input the printed price, calculate the discount, determine the price after the discount, compute the GST on this price, and sum it to get the final amount. This process can be implemented through simple mathematical operations in a Java program .

To compute the gross and net pay of an employee, first calculate the Dearness Allowance (DA) as 30% of the basic pay, the House Rent Allowance (HRA) as 15% of the basic pay, and the Provident Fund (PF) as 12.5% of the basic pay. Gross pay is calculated as the sum of basic pay, DA, and HRA. Net pay is the gross pay minus the PF. This can be implemented in Java by inputting the basic pay and using these formulas to compute and display the allowances, gross pay, and net pay .

The number of shares already held can be calculated by using the formula: number of shares = (Annual dividend * 100) / (Nominal value * div% division). To find how many more shares are needed to reach a target, deduct the number of shares held from the target shares. This is implemented in Java by inputting the annual dividend, using the given formula to calculate shares held, and subtracting from the target to find the additional shares needed .

Swapping two numbers without a third variable can be achieved using arithmetic operations: add both numbers to store their sum in one variable, subtract the second from the total to assign the original first number to it, and subtract the now updated second number from the total to restore the first number's original value in the first variable. This efficient method is preferred for saving memory, as it does not require extra space .

To convert time given in seconds to hours, minutes, and seconds, first divide the time by 3600 to get the hours. Then, take the remainder of the total seconds divided by 3600 to find minutes and further divide by 60 to separate minutes from seconds. This method of breakdown can be efficiently implemented in Java, using basic arithmetic operations .

Calculate the cost price using CP = SP/(1+profit/100) for profit and CP = SP/(1-loss/100) for loss. Add these two cost prices to get the total cost price of both calculators. This scenario demonstrates handling profits and losses simultaneously and is implemented effectively in Java by taking the selling price as input and applying these formulas .

The time period of a simple pendulum is calculated using the formula T = 2π√(l/g), where l is the length and g is the acceleration due to gravity. In Java, this can be implemented by taking l and g as inputs and computing T as 2 * Math.PI * Math.sqrt(l / g), then displaying the result formatted to two decimal places .

Shopkeeper A offers a single 30% discount, while Shopkeeper B offers two successive discounts of 20% and 10%. Calculate the final price for both shopkeepers by applying their respective discounts. For Shopkeeper A, subtract 30% directly, while for Shopkeeper B, apply a 20% discount followed by a 10% discount on the reduced price. Compare the final prices to determine that Shopkeeper B offers a slightly better deal due to the compounding effect of smaller sequential discounts .

You might also like