0% found this document useful (0 votes)
7 views

Java Programs

Java
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java Programs

Java
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1 2

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

// 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(System.in);
System.out.println(count + " 100-peso bill");
change %= 100; // Input two numbers
} System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
// Check and print the number of 50-peso bills
if (change >= 50) { System.out.print("Enter the second number: ");
int count = change / 50; double num2 = scanner.nextDouble();
System.out.println(count + " 50-peso bill");
change %= 50; // Display menu for operation selection
} System.out.println("Select an operation:");
System.out.println("1. Addition (+)");
// Check and print the number of 20-peso coins System.out.println("2. Subtraction (-)");
if (change >= 20) { System.out.println("3. Multiplication (*)");
int count = change / 20; System.out.println("4. Division (/)");
System.out.println(count + " 20-peso coin");
change %= 20; System.out.print("Enter your choice (1-4): ");
} int choice = scanner.nextInt();

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

import java.util.Scanner;

public class DiscountCalculator {


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

// Input the total purchase amount


System.out.print("Enter the total purchase amount: ");
double purchaseAmount = scanner.nextDouble();

double discount = 0;
// Calculate discount based on the purchase amount
if (purchaseAmount > 200) { int num1 = scanner.nextInt();
discount = 0.20; // 20% discount
} else if (purchaseAmount > 100) { System.out.print("Enter number two: ");
discount = 0.10; // 10% discount
} int num2 = scanner.nextInt();

// Calculate the total amount to be paid after applying the System.out.print("Enter number three: ");
discount int num3 = scanner.nextInt();
double discountAmount = purchaseAmount * discount;
double totalAmount = purchaseAmount - discountAmount; //================================//

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


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

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


System.out.print("Enter the number of kilowatt-hours
(kWh) used: "); else if (num2 > num1 && num2 > num3) {
double kWhUsed = scanner.nextDouble(); System.out.println("The largest number is: " + num2);
}
double totalBill = 0;
//================================//
// Calculate the bill based on the given rates else {
if (kWhUsed <= 100) { System.out.println("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


System.out.println("Total electricity bill: $" + totalBill); 8
}
}
import java.util.Scanner;
7
public class LeapYearChecker {
import java.util.Scanner; public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
public class LargestNumber {
// Input a year
public static void main(String[] args) { System.out.print("Enter a year: ");
Scanner scanner = new Scanner(System.in); int year = scanner.nextInt();

System.out.print("Enter number one: "); // Check if the year is a leap year


boolean isLeapYear = false; // Input destination
System.out.print("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 = scanner.next().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 (destination.equals("domestic")) {
} else { shippingCost = 5;
// If not divisible by 100, it's a leap year } else if (destination.equals("international")) {
isLeapYear = true; shippingCost = 10;
} }
} } else if (weight >= 1 && weight <= 5) {
// Output the result if (destination.equals("domestic")) {
if (isLeapYear) { shippingCost = 10;
System.out.println(year + " is a leap year."); } else if (destination.equals("international")) {
} else { shippingCost = 20;
System.out.println(year + " is not a leap year."); }
} } else if (weight > 5) {
} if (destination.equals("domestic")) {
} shippingCost = 15;
} else if (destination.equals("international")) {
shippingCost = 30;
}
}

// Output the shipping cost


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

9 10

import java.util.Scanner; import java.util.Scanner;

public class ShippingCostCalculator { public class FacebookLogin {


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

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

11 double totalDeduction = numOfAbsences *


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

System.out.print("Enter the number of subjects: "); }


int numSubjects = scanner.nextInt();

double totalGrades = 0.0;

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


System.out.print("Enter grade for subject " + i + ": ");
double grade = scanner.nextDouble();
totalGrades += grade;
}

double averageGrade = totalGrades / numSubjects;

System.out.println("Average Grade: " + averageGrade);

if (averageGrade >= 75) {


System.out.println("You Passed!");
} else {
System.out.println("You Failed!");
}
}
}

11

import java.util.Scanner;

public class TotalSalaryAfterDeduction {


public static void main(String[] args) {

Scanner scanner = new


Scanner(System.in);

System.out.print("Enter basic salary: ");


double basicSalary = scanner.nextDouble();

You might also like