[Go to site: main page, start]

0% found this document useful (0 votes)
5 views5 pages

Java Basic Calculator and Examples

Java
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)
5 views5 pages

Java Basic Calculator and Examples

Java
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

1 2

import [Link]; public class ArrayLengthExample {


public static void main(String[] args) {
public class Calculator { // Declare and initialise an array of integers
public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5};
Scanner scanner = new Scanner([Link]);
// Get the length of the array
// Input two numbers int lengthOfArray = [Link];
[Link]("Enter the first number: ");
double num1 = [Link](); // Display the length of the array
[Link]("The length of the array is: " +
[Link]("Enter the second number: "); lengthOfArray);
double num2 = [Link]();
// Print each element in the array using its length
// Display menu for operation selection [Link]("Array elements are:");
[Link]("Select an operation:"); for (int i = 0; i < [Link]; i++) {
[Link]("1. Addition (+)"); [Link]("Element at index " + i + ": " +
[Link]("2. Subtraction (-)"); numbers[i]);
[Link]("3. Multiplication (*)"); }
[Link]("4. Division (/)"); }
}
[Link]("Enter your choice (1-4): ");
int choice = [Link]();
3
// Perform the selected operation
switch (choice) { import [Link];
case 1: public class Main {
[Link]("Result: " + (num1 + num2)); public static void main(String[] args) {
break; Scanner scanner = new
case 2: Scanner([Link]);
[Link]("Result: " + (num1 - num2)); // Input
break; [Link]("Enter total: ");
case 3: int total = [Link]();
[Link]("Result: " + (num1 * num2)); [Link]("Enter payment: ");
break; int payment = [Link]();
case 4: //Calculations
if (num2 != 0) { int change = payment-total;
[Link]("Result: " + (num1 / num2)); // Conditional Statements
} else { if(change == 0) {
[Link]("Error: Division by zero is not [Link]("\nI receive exact amount.");
allowed."); return;
} }
break; if (change < 0) {
default: [Link]("Aicham! You have to pay
[Link]("Invalid choice. Please select a properly.");
number between 1 and 4."); return;
} }
} // Display the change
[Link]("Your change is " + change + ". Here's
}
your change:");
// Check and print the number of 1000-peso bills
if (change >= 1000) {
int count = change / 1000;
[Link](count + " 1000-peso bill");
change %= 1000;
}
// Check and print the number of 500-peso bills
if (change >= 500) {
int count = change / 500;
[Link](count + " 500-peso bill");
change %= 500;
}
// Check and print the number of 200-peso bills
if (change >= 200) {
int count = change / 200;
[Link](count + " 200-peso bill"); 4
change %= 200;
} import [Link];

// Check and print the number of 100-peso bills public class Calculator {
if (change >= 100) { public static void main(String[] args) {
int count = change / 100; Scanner scanner = new Scanner([Link]);
[Link](count + " 100-peso bill");
change %= 100; // Input two numbers
} [Link]("Enter the first number: ");
double num1 = [Link]();
// Check and print the number of 50-peso bills
if (change >= 50) { [Link]("Enter the second number: ");
int count = change / 50; double num2 = [Link]();
[Link](count + " 50-peso bill");
change %= 50; // Display menu for operation selection
} [Link]("Select an operation:");
[Link]("1. Addition (+)");
// Check and print the number of 20-peso coins [Link]("2. Subtraction (-)");
if (change >= 20) { [Link]("3. Multiplication (*)");
int count = change / 20; [Link]("4. Division (/)");
[Link](count + " 20-peso coin");
change %= 20; [Link]("Enter your choice (1-4): ");
} int choice = [Link]();

// Check and print the number of 10-peso coins // Perform the selected operation
if (change >= 10) { switch (choice) {
int count = change / 10; case 1:
[Link](count + " 10-peso coin"); [Link]("Result: " + (num1 + num2));
change %= 10; break;
} case 2:
[Link]("Result: " + (num1 - num2));
// Check and print the number of 5-peso coins break;
if (change >= 5) { case 3:
int count = change / 5; [Link]("Result: " + (num1 * num2));
[Link](count + " 5-peso coin"); break;
change %= 5; case 4:
} if (num2 != 0) {
[Link]("Result: " + (num1 / num2));
// Check and print the number of 1-peso coins } else {
if (change >= 1) { [Link]("Error: Division by zero is not
int count = change / 1; allowed.");
[Link](count + " 1-peso coin"); }
} break;
} default:
} [Link]("Invalid choice. Please select a
number between 1 and 4.");
}
}
}

import [Link];

public class DiscountCalculator {


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

// Input the total purchase amount


[Link]("Enter the total purchase amount: ");
double purchaseAmount = [Link]();

double discount = 0;
// Calculate discount based on the purchase amount
if (purchaseAmount > 200) { int num1 = [Link]();
discount = 0.20; // 20% discount
} else if (purchaseAmount > 100) { [Link]("Enter number two: ");
discount = 0.10; // 10% discount
} int num2 = [Link]();

// Calculate the total amount to be paid after applying the [Link]("Enter number three: ");
discount int num3 = [Link]();
double discountAmount = purchaseAmount * discount;
double totalAmount = purchaseAmount - discountAmount; //================================//

// Display the result if (num1 == num2 && num2 == num3) {


[Link]("Original purchase amount: $" + [Link]("There is no single largest
purchaseAmount); number.");
[Link]("Discount: $" + discountAmount); }
[Link]("Total amount to be paid: $" +
totalAmount); //================================//
}
} else if (num1 == num2 || num2 == num3 || num1 == num3) {
[Link]("There is no single largest number.");
}
6
//================================//
import [Link];
else {
public class ElectricityBillCalculator { if (num1 > num2 && num1 > num3) {
public static void main(String[] args) { [Link]("The largest number is: " + num1);
Scanner scanner = new Scanner([Link]); }

// Input the number of kilowatt-hours used //================================//


[Link]("Enter the number of kilowatt-hours
(kWh) used: "); else if (num2 > num1 && num2 > num3) {
double kWhUsed = [Link](); [Link]("The largest number is: " + num2);
}
double totalBill = 0;
//================================//
// Calculate the bill based on the given rates else {
if (kWhUsed <= 100) { [Link]("The largest number is: " + num3);
totalBill = kWhUsed * 0.10; // First 100 kWh at $0.10 }
per kWh }
} }
else if (kWhUsed <= 300) { }
totalBill = (100 * 0.10) + ((kWhUsed - 100) * 0.15); //
Next 200 kWh at $0.15 per kWh
}
else {
totalBill = (100 * 0.10) + (200 * 0.15) + ((kWhUsed -
300) * 0.20); // Above 300 kWh at $0.20 per kWh
}

// Display the total bill


[Link]("Total electricity bill: $" + totalBill); 8
}
}
import [Link];
7
public class LeapYearChecker {
import [Link]; public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
public class LargestNumber {
// Input a year
public static void main(String[] args) { [Link]("Enter a year: ");
Scanner scanner = new Scanner([Link]); int year = [Link]();

[Link]("Enter number one: "); // Check if the year is a leap year


boolean isLeapYear = false; // Input destination
[Link]("Enter the destination
// A year is a leap year if it is divisible by 4 (Domestic/International): ");
// and if divisible by 100, it should also be divisible by 400 String destination = [Link]().toLowerCase(); //
if (year % 4 == 0) { Convert to lowercase for easy comparison
if (year % 100 == 0) {
// If year is divisible by 100, it must also be divisible double shippingCost = 0;
by 400
if (year % 400 == 0) { // Calculate shipping cost based on weight and destination
isLeapYear = true; if (weight < 1) {
} if ([Link]("domestic")) {
} else { shippingCost = 5;
// If not divisible by 100, it's a leap year } else if ([Link]("international")) {
isLeapYear = true; shippingCost = 10;
} }
} } else if (weight >= 1 && weight <= 5) {
// Output the result if ([Link]("domestic")) {
if (isLeapYear) { shippingCost = 10;
[Link](year + " is a leap year."); } else if ([Link]("international")) {
} else { shippingCost = 20;
[Link](year + " is not a leap year."); }
} } else if (weight > 5) {
} if ([Link]("domestic")) {
} shippingCost = 15;
} else if ([Link]("international")) {
shippingCost = 30;
}
}

// Output the shipping cost


if (shippingCost > 0) {
[Link]("The shipping cost is: $" +
shippingCost);
} else {
[Link]("Invalid destination or weight
entered.");
}
}
}

9 10

import [Link]; import [Link];

public class ShippingCostCalculator { public class FacebookLogin {


public static void main(String[] args) { public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Scanner scanner = new Scanner([Link]);
// Input package weight
[Link]("Enter the weight of the package (in kg): String Username = "user";
"); String Password = "password";
double weight = [Link]();
[Link]("Enter your username: ");
String username = [Link](); [Link]("Enter total allowances: ");
double totalAllowances = [Link]();
[Link]("Enter your password: ");
String password = [Link](); double grossSalary = basicSalary + totalAllowances;

if ([Link](Username) &&
[Link](Password)) { [Link]("Your gloss salary is
[Link]("Login successful! Welcome to "+grossSalary);
Facebook.");
} else { [Link]("Enter number of absences: ");
[Link]("Login failed! Incorrect username or double numOfAbsences = [Link]();
password.");
} [Link]("Enter deduction per absences: ");
} double deductionPerAbsences =
} [Link]();

11 double totalDeduction = numOfAbsences *


deductionPerAbsences;
import [Link];
double grossPay = grossSalary - totalDeduction;
public class AverageGradeCalculator {
public static void main(String[] args) {
[Link]("Your gross pay after deduction:
"+ grossPay);
Scanner scanner = new Scanner([Link]);
}

[Link]("Enter the number of subjects: "); }


int numSubjects = [Link]();

double totalGrades = 0.0;

for (int i = 1; i <= numSubjects; i++) {


[Link]("Enter grade for subject " + i + ": ");
double grade = [Link]();
totalGrades += grade;
}

double averageGrade = totalGrades / numSubjects;

[Link]("Average Grade: " + averageGrade);

if (averageGrade >= 75) {


[Link]("You Passed!");
} else {
[Link]("You Failed!");
}
}
}

11

import [Link];

public class TotalSalaryAfterDeduction {


public static void main(String[] args) {

Scanner scanner = new


Scanner([Link]);

[Link]("Enter basic salary: ");


double basicSalary = [Link]();

You might also like