Program 1:
Solution:
import java.util.Scanner;
class Eshop {
//
String name;
double price;
double netAmount;
//
void accept() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name of the item: ");
name = sc.nextLine();
System.out.print("Enter the price of the item: ");
price = sc.nextDouble();
}
//
void calculate() {
double discount = 0;
if (price >= 1000 && price <= 25000) {
discount = 0.05;
} else if (price >= 25001 && price <= 57000) {
discount = 0.075;
} else if (price >= 57001 && price <= 100000) {
discount = 0.10;
} else if (price > 100000) {
discount = 0.15;
}
netAmount = price - (price * discount);
}
//
void display() {
System.out.println("Item Name: " + name);
System.out.println("Net Amount to be paid: ₹" + netAmount);
}
//
public static void main(String[] args) {
Eshop obj = new Eshop();
obj.accept();
obj.calculate();
obj.display();
}
}
Program 2:
Design a program in Java to calculate the discount given to a customer on purchasing LED Television. The
program also displays the amount paid by the customer after discount. The details are given as:
• Class name: Television
• Data members: int cost, int discount, int amount
• Member methods:
o accept() : to input the cost of Television
o calculate() : to calculate the discount
o display() : to show the discount and the amount paid after discount
o
Solution:
import java.util.Scanner;
class Television {
//
int cost;
int discount;
int amount;
//
void accept() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the cost of the Television: ");
cost = sc.nextInt();
}
//
void calculate() {
// Let's assume a flat 10% discount for demonstration
discount = (int)(cost * 0.10);
amount = cost - discount;
}
//
void display() {
System.out.println("Discount: ₹" + discount);
System.out.println("Amount to be paid after discount: ₹" + amount);
}
//
public static void main(String[] args) {
Television tv = new Television();
tv.accept();
tv.calculate();
tv.display();
}
}
Program 3:
In an election, there are two candidates X and Y. On the election day, 80% of the voters go for polling, out of
which 60% vote for X. Write a program to take the number of voters as input and calculate:
(a) number of votes received by X
(b) number of votes received by Y
Solution:
import java.util.Scanner;
public class Election {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//
System.out.print("Enter the total number of voters: ");
int totalVoters = sc.nextInt();
//
int votersPolled = (int)(totalVoters * 0.80);
//
int votesForX = (int)(votersPolled * 0.60);
//
int votesForY = votersPolled - votesForX;
//
System.out.println("Number of votes received by Candidate X: " + votesForX);
System.out.println("Number of votes received by Candidate Y: " + votesForY);
}
}
Program 4:
A shopkeeper offers 10% discount on the printed price of a mobile phone. However, a customer has to pay 9%
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.
Solution:
import java.util.Scanner;
public class MobilePriceCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//
System.out.print("Enter the printed price of the mobile phone: ");
double printedPrice = sc.nextDouble();
//
double discount = printedPrice * 0.10;
double discountedPrice = printedPrice - discount;
//
double gst = discountedPrice * 0.09;
double finalAmount = discountedPrice + gst;
//
System.out.println("Discounted Price: ₹" + discountedPrice);
System.out.println("GST (9%): ₹" + gst);
System.out.println("Total Amount to be Paid: ₹" + finalAmount);
}
}
Program 5:
A man spends ½ of his salary on food, ¹⁄₁₅ on rent, and ¹⁄₁₀ on miscellaneous activities. The rest of the salary is
his saving.
Write a program to calculate and display the following:
(a) Money spent on food
(b) Money spent on rent
(c) Money spent on miscellaneous activities
(d) Money saved
Take the salary as an input.
Solution:
import java.util.Scanner;
public class SalaryBreakdown {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input salary
System.out.print("Enter the salary: ");
double salary = sc.nextDouble();
// Calculations
double food = salary * 1.0 / 2;
double rent = salary * 1.0 / 15;
double misc = salary * 1.0 / 10;
double saved = salary - (food + rent + misc);
// Output
System.out.println("Money spent on food: ₹" + food);
System.out.println("Money spent on rent: ₹" + rent);
System.out.println("Money spent on miscellaneous activities: ₹" + misc);
System.out.println("Money saved: ₹" + saved);
}
}
Program 6:
Write a program to input time in seconds. Display the time after converting it into hours, minutes, and
seconds.
Sample Input: Time in seconds: 5420
Sample Output: 1 Hour 30 Minutes 20 Seconds
Solution:
import java.util.Scanner;
public class TimeConverter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input total seconds
System.out.print("Enter time in seconds: ");
int totalSeconds = sc.nextInt();
// Convert to hours, minutes, and seconds
int hours = totalSeconds / 3600;
int remainingSeconds = totalSeconds % 3600;
int minutes = remainingSeconds / 60;
int seconds = remainingSeconds % 60;
// Output the result
System.out.println(hours + " Hour " + minutes + " Minutes " + seconds + " Seconds");
}
}
Program 7:
The driver took a drive to a town 240 km at a speed of 60 km/h. Later in the evening, he drove back at 20 km/h
less than the usual speed. Write a program to calculate:
(a) the total time taken by the driver
(b) the average speed during the whole journey
Solution:
public class DriverJourney {
public static void main(String[] args) {
double distance = 240; // Distance one way in km
double speedOnward = 60; // Speed while going
double speedReturn = speedOnward - 20; // Speed while returning
// Time = Distance / Speed
double timeOnward = distance / speedOnward;
double timeReturn = distance / speedReturn;
double totalTime = timeOnward + timeReturn;
double totalDistance = 2 * distance;
double averageSpeed = totalDistance / totalTime;
System.out.println("Total time taken by the driver: " + totalTime + " hours");
System.out.println("Average speed during the whole journey: " + averageSpeed + " km/h");
}
}
Program 8:
Write a program to input two unequal numbers. Display the numbers after swapping their values in the
variables without using a third variable.
Solution:
import java.util.Scanner;
public class SwapWithoutThirdVar {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter value of a: ");
int a = sc.nextInt();
System.out.print("Enter value of b: ");
int b = sc.nextInt();
// Swapping without third variable
a = a + b;
b = a - b;
a = a - b;
System.out.println("After swapping:");
System.out.println("a = " + a + ", b = " + b);
sc.close();
}
}
Program 9 :
A certain amount of money is invested for 3 years at the rates of 6%, 8%, and 10% per annum compounded
annually. Write a program to calculate:
1. The amount after 3 years.
2. The compound interest after 3 years.
Accept the principal amount (initial investment) as input.
Solution :
import java.util.Scanner;
public class CompoundInterestCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Accept principal amount as input
System.out.print("Enter the principal amount: ");
double principal = scanner.nextDouble();
// Define interest rates and time period
double rate1 = 6.0;
double rate2 = 8.0;
double rate3 = 10.0;
int time = 1; // Each rate is applied for 1 year
// Calculate the amount after 3 years
double amount = principal * Math.pow((1 + rate1 / 100), time)
* Math.pow((1 + rate2 / 100), time)
* Math.pow((1 + rate3 / 100), time);
// Calculate compound interest
double compoundInterest = amount - principal;
// Output the results
System.out.println("Amount after 3 years: " + amount);
System.out.println("Compound interest after 3 years: " + compoundInterest);
}
}
Program 10:
The coordinates of two points A and B on a straight line are given as (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2).
Write a program to calculate the slope mm of the line using the formula:
Slope = (y2-y1)/(x2-x1)
Take the coordinates (x1,y1) and (x2,y2) as the input:
Solution:
import java.util.Scanner;
public class SlopeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//
System.out.print("Enter x1: ");
double x1 = scanner.nextDouble();
System.out.print("Enter y1: ");
double y1 = scanner.nextDouble();
System.out.print("Enter x2: ");
double x2 = scanner.nextDouble();
System.out.print("Enter y2: ");
double y2 = scanner.nextDouble();
// Checking if the line is vertical
if (x1 == x2) {
System.out.println("The line is vertical, and the slope is undefined.");
} else {
//
double slope = (y2 - y1) / (x2 - x1);
System.out.println("Slope of the line: " + slope);
}
scanner.close();
}
}
Program 11:
A dealer allows his customers a discount of 25% and still gains 25%. Write a program to input the cost of an
article and display its selling price and marked price .
Solution :
import java.util.Scanner;
public class PriceCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Accept cost price from the user
System.out.print("Enter the cost price of the article: ");
double costPrice = scanner.nextDouble();
// Given gain percentage
double gainPercentage = 25.0;
double discountPercentage = 25.0;
// Selling price calculation
double sellingPrice = costPrice * (1 + gainPercentage / 100);
// Marked price calculation based on 25% discount
double markedPrice = sellingPrice / (1 - discountPercentage / 100);
// Displaying results
System.out.println("Selling Price: ₹" + sellingPrice);
System.out.println("Marked Price: ₹" + markedPrice);
scanner.close();
}
}