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

JAVA (22BCE10522) Aditya Vikram Singh

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)
7 views4 pages

JAVA (22BCE10522) Aditya Vikram Singh

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
You are on page 1/ 4

2nd Assignment

1. Write a program that prompts the user for two integers and then displays
the
sum, the difference, the product, the average, the absolute value of the
difference, the larger of the two, and the smaller of the two.

ANS)

import java.util.Scanner;

public class ArithmeticOperations {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first integer: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second integer: ");
int num2 = scanner.nextInt();
int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
double average = (num1 + num2) / 2.0;
int absDifference = Math.abs(difference);
int larger = Math.max(num1, num2);
int smaller = Math.min(num1, num2);
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Average: " + average);
System.out.println("Absolute Value of the Difference: " + absDifference);
System.out.println("Larger of the Two: " + larger);
System.out.println("Smaller of the Two: " + smaller);
}
}
2. Write a Java program that asks the user to enter the price of a car (real), the
down payment (real) and the number of years for the loan (integer). Calculate
and output the monthly payment (real) of that car. Assume the sales tax rate
is 7% and an interest rate is 9%. Use constant declarations for these rates.
ANS)

import java.util.Scanner;

public class CarLoanCalculator {


private static final double SALES_TAX_RATE = 0.07;
private static final double INTEREST_RATE = 0.09;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the price of the car: ");
double carPrice = scanner.nextDouble();
System.out.print("Enter the down payment: ");
double downPayment = scanner.nextDouble();
System.out.print("Enter the number of years for the loan: ");
int years = scanner.nextInt();
double loanAmount = carPrice - downPayment;

double totalAmount = loanAmount + (loanAmount * SALES_TAX_RATE);

int months = years * 12;


double monthlyInterestRate = INTEREST_RATE / 12;
double monthlyPayment = (totalAmount * monthlyInterestRate)
(1 - Math.pow(1 + monthlyInterestRate, -months))
System.out.printf("The monthly payment is: %.2f\n", monthlyPayment);
}
}
3. Wages are paid at a standard hourly rate for 40 hours per week and at time
and one half for overtime. Write a Java program that calculates and displays
the weekly pay, given the standard wage amount and the number of hours of
overtime as inputs.
ANS)
import java.util.Scanner;

public class WeeklyPayCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the standard hourly wage: ");
double hourlyWage = scanner.nextDouble();
System.out.print("Enter the number of overtime hours: ");
int overtimeHours = scanner.nextInt();
Final int STANDARD_HOURS = 40;
double standardPay = hourlyWage * STANDARD_HOURS;
double overtimePay = overtimeHours * (hourlyWage * 1.5);
double weeklyPay = standardPay + overtimePay;
System.out.printf("The weekly pay is: %.2f\n", weeklyPay);
}
}

You might also like