0% found this document useful (0 votes)
7 views11 pages

Input in Java

The document contains a series of Java programming tasks, each with a specific problem statement and corresponding code implementation. The tasks include calculating the time period of a pendulum, employee pay, discounts on products, compound interest, share calculations, time conversions, number swapping, and the difference between compound and simple interest. Each task is structured to take user input and display the results based on the calculations performed in the Java code.

Uploaded by

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

Input in Java

The document contains a series of Java programming tasks, each with a specific problem statement and corresponding code implementation. The tasks include calculating the time period of a pendulum, employee pay, discounts on products, compound interest, share calculations, time conversions, number swapping, and the difference between compound and simple interest. Each task is structured to take user input and display the results based on the calculations performed in the Java code.

Uploaded by

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

INPUT IN JAVA

Question 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 and
acceleration due to gravity (g) as inputs.
import [Link];

public class SimplePendulum


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter length: ");
double l = [Link]();
[Link]("Enter g: ");
double g = [Link]();
double t = 2 * (22.0 / 7.0) * [Link](l/g);
[Link]("T = " + t);
}
}

Question 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 Employee


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter Basic Pay: ");
double bp = [Link]();
double da = 0.3 * bp;
double hra = 0.15 * bp;
double pf = 0.125 * bp;
double gp = bp + da + hra;
double np = gp - pf;
[Link]("Gross Pay = " + gp);
[Link]("Net Pay = " + np);
}
}
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 [Link];

public class CameraPrice


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter printed price of Digital Camera:");
double mrp = [Link]();
double disc = mrp * 10 / 100.0;
double price = mrp - disc;
double gst = price * 6 / 100.0;
price += gst;
[Link]("Amount to be paid: " + price);
}
}

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 [Link];

public class Discounts


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter price of article: ");
double price = [Link]();

double d1 = price * 30 / 100.0;


double amt1 = price - d1;
[Link]("30% discount = " + d1);
[Link]("Amount after 30% discount = " + amt1);

double d2 = price * 20 / 100.0;


double amt2 = price - d2;
double d3 = amt2 * 10 / 100.0;
amt2 -= d3;
[Link]("20% discount = " + d2);
[Link]("10% discount = " + d3);
[Link]("Amount after successive discounts = " + amt2);
}
}
Question 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 amount after three years.
Take sum as an input from the user.
Sample Input: Principal = ₹5000, Rate =10%, Time = 3 yrs
Sample Output: Interest for the first year: ₹500
Interest for the second year: ₹550
Interest for the third year: ₹605
import [Link];

public class CompoundInterest


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter sum of money: ");
double p = [Link]();
double interest = p * 5 * 1 / 100.0;
[Link]("Interest for the first year = " + interest);
p += interest;
interest = p * 5 * 1 / 100.0;
[Link]("Interest for the second year = " + interest);
p += interest;
interest = p * 5 * 1 / 100.0;
[Link]("Interest for the third year = " + interest);
}
}

Question 6
A businessman wishes to accumulate 3000 shares of a company. However, he already has
some shares of that company valuing Rs.10 (nominal value) which yield 10% dividend per
annum and receive Rs.2000 as dividend at the end of the year. Write a program in Java to
calculate the number of shares he has and how many more shares to be purchased to make
his target.
Hint: No. of share = (Annual dividend * 100) / (Nominal value * div%)
public class Shares
{
public static void main(String args[]) {
int sharesHeld = (2000 * 100)/(10 * 10);
[Link]("No. of shares held currently = "
+ sharesHeld);
int sharesRequired = 3000 - sharesHeld;
[Link]("No. of shares to purchase = "
+ sharesRequired);
}
}
Question 7
Write a program to input the 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 TimeConvert


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter time in seconds: ");
long secs = [Link]();
long hrs = secs / 3600;
secs %= 3600;
long mins = secs / 60;
secs %= 60;
[Link](hrs + " Hours " + mins
+ " Minutes " + secs + " Seconds");
}
}

Question 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 NumberSwap


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter two unequal numbers");
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
a = a + b;
b = a - b;
a = a - b;
[Link]("a = " + a + " b = " + b);
}
}
Question 9
A certain amount is invested at the rate 10% per annum for 3 years. Find the difference
between Compound Interest (CI) and Simple Interest (SI). Write a program to take amount
as an input.
Hint: SI = (P * R * T) / 100
A = P * (1 + (R/100)) T
CI = A - P
import [Link];

public class InterestDifference


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter Amount: ");
double p = [Link]();
double si = p * 10 * 3 / 100;
double ciAmt = p * [Link](1 + (10/100.0), 3);
double ci = ciAmt - p;
[Link]("Difference between CI & SI: " + (ci - si));
}
}

Question 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 his total cost price of the
calculators by taking selling price as input.
Hint: CP = (SP / (1 + (profit / 100))) (when profit)
CP = (SP / (1 - (loss / 100))) (when loss)
import [Link];

public class Shopkeeper


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the selling price: ");
double sp = [Link]();
double cp1 = (sp / (1 + (20 / 100.0)));
double cp2 = (sp / (1 - (20 / 100.0)));
double totalCP = cp1 + cp2;
[Link]("Total Cost Price = " + totalCP);
}
}
INPUT IN JAVA

Question 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 and
acceleration due to gravity (g) as inputs.
import [Link];

public class SimplePendulum


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter length: ");
double l = [Link]();
[Link]("Enter g: ");
double g = [Link]();
double t = 2 * (22.0 / 7.0) * [Link](l/g);
[Link]("T = " + t);
}
}

Question 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 Employee


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter Basic Pay: ");
double bp = [Link]();
double da = 0.3 * bp;
double hra = 0.15 * bp;
double pf = 0.125 * bp;
double gp = bp + da + hra;
double np = gp - pf;
[Link]("Gross Pay = " + gp);
[Link]("Net Pay = " + np);
}
}
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 [Link];

public class CameraPrice


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter printed price of Digital Camera:");
double mrp = [Link]();
double disc = mrp * 10 / 100.0;
double price = mrp - disc;
double gst = price * 6 / 100.0;
price += gst;
[Link]("Amount to be paid: " + price);
}
}

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 [Link];

public class Discounts


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter price of article: ");
double price = [Link]();

double d1 = price * 30 / 100.0;


double amt1 = price - d1;
[Link]("30% discount = " + d1);
[Link]("Amount after 30% discount = " + amt1);

double d2 = price * 20 / 100.0;


double amt2 = price - d2;
double d3 = amt2 * 10 / 100.0;
amt2 -= d3;
[Link]("20% discount = " + d2);
[Link]("10% discount = " + d3);
[Link]("Amount after successive discounts = " + amt2);
}
}
Question 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 amount after three years.
Take sum as an input from the user.
Sample Input: Principal = ₹5000, Rate =10%, Time = 3 yrs
Sample Output: Interest for the first year: ₹500
Interest for the second year: ₹550
Interest for the third year: ₹605
import [Link];

public class CompoundInterest


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter sum of money: ");
double p = [Link]();
double interest = p * 5 * 1 / 100.0;
[Link]("Interest for the first year = " + interest);
p += interest;
interest = p * 5 * 1 / 100.0;
[Link]("Interest for the second year = " + interest);
p += interest;
interest = p * 5 * 1 / 100.0;
[Link]("Interest for the third year = " + interest);
}
}

Question 6
A businessman wishes to accumulate 3000 shares of a company. However, he already has
some shares of that company valuing Rs.10 (nominal value) which yield 10% dividend per
annum and receive Rs.2000 as dividend at the end of the year. Write a program in Java to
calculate the number of shares he has and how many more shares to be purchased to make
his target.
Hint: No. of share = (Annual dividend * 100) / (Nominal value * div%)
public class Shares
{
public static void main(String args[]) {
int sharesHeld = (2000 * 100)/(10 * 10);
[Link]("No. of shares held currently = "
+ sharesHeld);
int sharesRequired = 3000 - sharesHeld;
[Link]("No. of shares to purchase = "
+ sharesRequired);
}
}
Question 7
Write a program to input the 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 TimeConvert


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter time in seconds: ");
long secs = [Link]();
long hrs = secs / 3600;
secs %= 3600;
long mins = secs / 60;
secs %= 60;
[Link](hrs + " Hours " + mins
+ " Minutes " + secs + " Seconds");
}
}

Question 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 NumberSwap


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter two unequal numbers");
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
a = a + b;
b = a - b;
a = a - b;
[Link]("a = " + a + " b = " + b);
}
}
Question 9
A certain amount is invested at the rate 10% per annum for 3 years. Find the difference
between Compound Interest (CI) and Simple Interest (SI). Write a program to take amount
as an input.
Hint: SI = (P * R * T) / 100
A = P * (1 + (R/100)) T
CI = A - P
import [Link];

public class InterestDifference


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter Amount: ");
double p = [Link]();
double si = p * 10 * 3 / 100;
double ciAmt = p * [Link](1 + (10/100.0), 3);
double ci = ciAmt - p;
[Link]("Difference between CI & SI: " + (ci - si));
}
}

Question 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 his total cost price of the
calculators by taking selling price as input.
Hint: CP = (SP / (1 + (profit / 100))) (when profit)
CP = (SP / (1 - (loss / 100))) (when loss)
import [Link];

public class Shopkeeper


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the selling price: ");
double sp = [Link]();
double cp1 = (sp / (1 + (20 / 100.0)));
double cp2 = (sp / (1 - (20 / 100.0)));
double totalCP = cp1 + cp2;
[Link]("Total Cost Price = " + totalCP);
}
}

You might also like