0% found this document useful (0 votes)
2 views10 pages

Solution

The document contains multiple Java programs that perform various tasks, including calculating discounts, cab charges, checking Dudeney numbers, generating patterns, classifying numbers, and calculating remaining days in a year. Each program includes user input, conditional logic, and output of results. Additionally, there are explanations of the logic behind each program's functionality.

Uploaded by

tanmaykedia2010
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)
2 views10 pages

Solution

The document contains multiple Java programs that perform various tasks, including calculating discounts, cab charges, checking Dudeney numbers, generating patterns, classifying numbers, and calculating remaining days in a year. Each program includes user input, conditional logic, and output of results. Additionally, there are explanations of the logic behind each program's functionality.

Uploaded by

tanmaykedia2010
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

import [Link].

Scanner;

public class DiscountCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

// Accept product price and number of units sold


[Link]("Enter the product price: ");
double price = [Link]();

[Link]("Enter the number of units sold: ");


int units = [Link]();

// Calculate total cost


double totalCost = price * units;
double discountRate = 0;

// Determine discount rate based on the provided table


if (totalCost >= 100000) {
discountRate = 8.2;
} else if (totalCost >= 50000) {
discountRate = 6.0;
} else if (totalCost >= 25000) {
discountRate = 3.5;
} else if (totalCost >= 10000) {
discountRate = 2.0;
} else {
discountRate = 1.0;
}

// Calculate discount amount and amount payable


double discountAmount = (discountRate / 100) * totalCost;
double amountPayable = totalCost - discountAmount;

// Output results
[Link]("\n--- Invoice Details ---");
[Link]("Total Cost: Rs. " + totalCost);
[Link]("Discount Rate Applied: " + discountRate + "%");
[Link]("Discount Amount: Rs. " + discountAmount);
[Link]("Net Amount Payable: Rs. " + amountPayable);

[Link]();
}
}

How the Logic Works

 Input Acceptance: The program uses the Scanner class to take the product price and
quantity from the user.
 Total Cost Calculation: It first calculates the base cost before any discounts are
applied.
 Conditional Logic: It uses an if-else if ladder to check the totalCost against the
thresholds provided in your table (e.g., 8.2% for costs over Rs. 100,000).
 Final Output: It subtracts the calculated discount from the total to give you the final
"Amount Payable".

Based on the logic required for Question 4 in the provided images, here is a Java program
that uses the slab-based calculation method to determine cab charges.

Java
import [Link];

public class Cab_Service {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Input customer details


[Link]("Enter Customer Name: ");
String customerName = [Link]();

[Link]("Enter kilometers travelled: ");


double km = [Link]();

double totalCharge = 0;
// Slab-based calculation logic
if (km <= 50) {
totalCharge = km * 3.50;
} else if (km <= 75) { // First 50 + Next 25
totalCharge = (50 * 3.50) + ((km - 50) * 5.00);
} else if (km <= 125) { // First 75 + Next 50
totalCharge = (50 * 3.50) + (25 * 5.00) + ((km - 75) * 6.50);
} else { // Above 125
totalCharge = (50 * 3.50) + (25 * 5.00) + (50 * 6.50) + ((km -
125) * 9.00);
}

// Additional fuel hike charge


totalCharge += 50.00;

// Output results
[Link]("\n--- Cab Service Invoice ---");
[Link]("Customer Name: " + customerName);
[Link]("Distance Travelled: " + km + " km");
[Link]("Total Amount (including Rs. 50 fuel hike): Rs.
" + totalCharge);

[Link]();
}
}

Breakdown of the Slab Logic

The program follows the specific rules outlined in the table:

 First 50 km: Charged at Rs. 3.5/km.


 Next 25 km: Charged at Rs. 5.0/km.
 Next 50 km: Charged at Rs. 6.5/km.
 Above 125 km: Charged at Rs. 9.0/km.
 Fixed Fee: A mandatory surcharge of Rs. 50.00 is added at the end for fuel price
hikes.
Based on the requirements for Question 5 in the provided images, here
is a Java program to check if a number is a Dudeney number.

Definition

A Dudeney number is a positive integer that is a perfect cube such that


the sum of its digits is equal to the cube root of the number. For example,
512 is a Dudeney number because $5 + 1 + 2 = 8$, and $\sqrt[3]{512} =
8$.

Java
import [Link];

public class DudeneyCheck {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Input the number from the user


[Link]("Enter a positive integer: ");
int num = [Link]();

int sumOfDigits = 0;
int temp = num;

// Calculate the sum of digits


while (temp > 0) {
int digit = temp % 10;
sumOfDigits += digit;
temp = temp / 10;
}

// Calculate the cube root of the number


// [Link] is used to handle potential precision issues with double
double cubeRoot = [Link]([Link](num, 1.0/3.0));

// Display results and check the condition


[Link]("Sum of digits = " + sumOfDigits);
[Link]("Cube root of " + num + " = " + (int)cubeRoot);

if (sumOfDigits == cubeRoot) {
[Link]("As Sum of digits = Cube root of Number.");
[Link]("Hence " + num + " is a Dudeney number.");
} else {
[Link]("Hence " + num + " is NOT a Dudeney number.");
}

[Link]();
}
}

How the Logic Works

1. Digit Extraction: The while loop uses the modulo operator % 10 to


strip the last digit and adds it to sumOfDigits.
2. Cube Root Calculation: It uses [Link](num, 1.0/3.0) to find the
cube root. I have included [Link] to ensure that if the result is
something like 7.99999999, it correctly evaluates to 8.
3. Comparison: The program compares the calculated sum with the
cube root to print the final verdict.

Based on the requirements for Question 6 in the provided images, here


is a menu-driven Java program that uses a switch case to handle both the
pattern generation and the series summation.

Java
import [Link];

public class MenuDrivenProgram {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Displaying the menu


[Link]("--- Menu ---");
[Link]("1. Display the number pattern");
[Link]("2. Display the sum of the series (Squares up to 64)");
[Link]("Enter your choice (1 or 2): ");

int choice = [Link]();

switch (choice) {
case 1:
// Task 1: Display the pattern
// The pattern starts at 7 and decreases by 2 each row (7, 5, 3, 1)
[Link]("\nPattern:");
for (int i = 7; i >= 1; i -= 2) {
for (int j = 1; j <= i; j++) {
[Link](j + " ");
}
[Link]();
}
break;

case 2:
// Task 2: Display the sum of the series
// Series: 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 (Squares of 1 to 8)
int sum = 0;
[Link]("\nSeries: ");
for (int i = 1; i <= 8; i++) {
int square = i * i;
sum += square;
[Link](square + (i < 8 ? " + " : ""));
}
[Link]("\nTotal Sum = " + sum);
break;

default:
// Mandatory default case if none of the choice matches
[Link]("\nInvalid choice! Please enter either 1 or 2.");
break;
}

[Link]();
}
}
Logic Breakdown

1. The Pattern (Choice 1):

 The pattern consists of four rows starting with 7 numbers and


reducing by 2 each time.
 The outer loop for (int i = 7; i >= 1; i -= 2) controls the number of
elements in each row.
 The inner loop prints numbers from 1 up to the current value of i.

2. The Series (Choice 2):

 The series $1+4+9+16+25+36+49+64$ represents the squares of


numbers from 1 to 8.
 The loop runs from 1 to 8, calculates the square of each number,
adds it to a sum variable, and prints the process.

3. Default Case:

 As specified in the problem, a default case is included to handle any


user input that is not 1 or 2.
To solve the problem described in Question 7, you need a program that accepts a series of
integers from a user and categorizes them into three different sums. The loop should continue
until the user enters 0, which acts as the termination signal.

Java Implementation

Java
import [Link];

public class NumberClassifier {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

int sumNegative = 0;
int sumPositiveEven = 0;
int sumPositiveOdd = 0;
int n;

[Link]("Enter numbers (enter 0 to terminate):");

// The list terminates when the user enters zero


while (true) {
n = [Link]();

if (n == 0) {
break; // Exit the loop when 0 is entered
}

if (n < 0) {
// Sum of negative numbers
sumNegative += n;
} else {
// Check if the positive number is even or odd
if (n % 2 == 0) {
// Sum of positive even numbers
sumPositiveEven += n;
} else {
// Sum of positive odd numbers
sumPositiveOdd += n;
}
}
}

// Printing the results


[Link]("\n--- Results ---");
[Link]("Sum of negative numbers: " + sumNegative);
[Link]("Sum of positive even numbers: " +
sumPositiveEven);
[Link]("Sum of positive odd numbers: " +
sumPositiveOdd);

[Link]();
}
}

Logic Breakdown
 Termination: The program uses a while(true) loop that checks if the input n is 0. If
it is, the break statement ends the loop immediately.
 Condition 1 (Negative): If the number is less than 0, it is added to sumNegative.
 Condition 2 (Positive Even): If the number is greater than 0 and divisible by 2 (n %
2 == 0), it is added to sumPositiveEven.
 Condition 3 (Positive Odd): If the number is greater than 0 and not divisible by 2, it
is added to sumPositiveOdd.

Based on the requirements in Question 8 from your uploaded images, here is a Java program
that calculates the remaining days in a year starting from a given month. It uses a switch
case with fall-through logic to sum the days and includes a check for leap years.

Java Implementation

Java
import [Link];

public class RemainingDaysCalculator {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Input year and month number


[Link]("Enter the year: ");
int year = [Link]();
[Link]("Enter the month number (1-12): ");
int month = [Link]();
// Check for leap year
boolean isLeap = false;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
isLeap = true;
}

int febDays = isLeap ? 29 : 28;


int remainingDays = 0;

// Using switch case with fall-through logic to sum remaining days


// We start from the input month and add days for all subsequent
months
switch (month) {
case 1: remainingDays += 31; // January
case 2: remainingDays += febDays; // February
case 3: remainingDays += 31; // March
case 4: remainingDays += 30; // April
case 5: remainingDays += 31; // May
case 6: remainingDays += 30; // June
case 7: remainingDays += 31; // July
case 8: remainingDays += 31; // August
case 9: remainingDays += 30; // September
case 10: remainingDays += 31; // October
case 11: remainingDays += 30; // November
case 12: remainingDays += 31; // December
break;
default:
[Link]("Invalid month number entered.");
return;
}

// Output results
[Link]("\nNumber of days remaining in the year is: " +
remainingDays + " days");
if (isLeap) {
[Link]("The year " + year + " is a leap year.");
} else {
[Link]("The year " + year + " is not a leap
year.");
}

[Link]();
}
}

Logic Breakdown

 Leap Year Check: A year is a leap year if it is divisible by 4 but not by 100, or if it is
divisible by 400. This determines if February has 28 or 29 days.
 Switch Case Fall-through: By omitting the break statement after each month, the
program starts at the user's chosen month and "falls through" all subsequent cases,
adding the days for every month until the end of the year.
 Example (2004, Month 2): If the user enters month 2, the program adds febDays
(29), then falls into case 3 (+31), case 4 (+30), and so on, resulting in the 335 days
shown in your example.

You might also like