diff --git a/Lab2/Excecise_12_Perfect_Num_First100000.java b/Lab2/Excecise_12_Perfect_Num_First100000.java new file mode 100644 index 0000000..88742a9 --- /dev/null +++ b/Lab2/Excecise_12_Perfect_Num_First100000.java @@ -0,0 +1,15 @@ +package Lab2; + +public class Excecise_12_Perfect_Num_First100000 { + public static void main(String[] args) { + for (int i = 1; i <= 100000; i++) { + int sum = 0; + for (int j = 1; j <= i / 2; j++) { + if (i % j == 0) sum += j; + } + if (sum == i) { + System.out.println(i + " is a Perfect Number."); + } + } + } +} diff --git a/Lab2/Exercise_1_FahrenheitToCelsius.java b/Lab2/Exercise_01_FahrenheitToCelsius.java similarity index 87% rename from Lab2/Exercise_1_FahrenheitToCelsius.java rename to Lab2/Exercise_01_FahrenheitToCelsius.java index 3fec90e..40e6539 100644 --- a/Lab2/Exercise_1_FahrenheitToCelsius.java +++ b/Lab2/Exercise_01_FahrenheitToCelsius.java @@ -2,7 +2,7 @@ import java.util.Scanner; -public class Exercise_1_FahrenheitToCelsius { +public class Exercise_01_FahrenheitToCelsius { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter Fahrenheit: "); diff --git a/Lab2/Exercise_2_CylinderVolume.java b/Lab2/Exercise_02_CylinderVolume.java similarity index 92% rename from Lab2/Exercise_2_CylinderVolume.java rename to Lab2/Exercise_02_CylinderVolume.java index 00f024b..3c81394 100644 --- a/Lab2/Exercise_2_CylinderVolume.java +++ b/Lab2/Exercise_02_CylinderVolume.java @@ -2,7 +2,7 @@ import java.util.Scanner; -public class Exercise_2_CylinderVolume { +public class Exercise_02_CylinderVolume { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the radius: "); diff --git a/Lab2/Exercise_3_SumOfDigits.java b/Lab2/Exercise_03_SumOfDigits.java similarity index 91% rename from Lab2/Exercise_3_SumOfDigits.java rename to Lab2/Exercise_03_SumOfDigits.java index a335734..2ffe47a 100644 --- a/Lab2/Exercise_3_SumOfDigits.java +++ b/Lab2/Exercise_03_SumOfDigits.java @@ -2,7 +2,7 @@ import java.util.Scanner; -public class Exercise_3_SumOfDigits { +public class Exercise_03_SumOfDigits { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a number between 0 and 1000: "); diff --git a/Lab2/Exercise_04_Uppercase_Letter_to_Lowercase.java b/Lab2/Exercise_04_Uppercase_Letter_to_Lowercase.java new file mode 100644 index 0000000..4098188 --- /dev/null +++ b/Lab2/Exercise_04_Uppercase_Letter_to_Lowercase.java @@ -0,0 +1,13 @@ +package Lab2; + +import java.util.Scanner; + +public class Exercise_04_Uppercase_Letter_to_Lowercase { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter an uppercase letter: "); + char upper = input.next().charAt(0); + char lower = Character.toLowerCase(upper); + System.out.println("Lowercase letter: " + lower); + } +} \ No newline at end of file diff --git a/Lab2/Exercise_05_Character_to_ASCII.java b/Lab2/Exercise_05_Character_to_ASCII.java new file mode 100644 index 0000000..7b7b8c8 --- /dev/null +++ b/Lab2/Exercise_05_Character_to_ASCII.java @@ -0,0 +1,13 @@ +package Lab2; + +import java.util.Scanner; + +public class Exercise_05_Character_to_ASCII { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter an ASCII code (0 to 128): "); + int ascii = input.nextInt(); + char character = (char) ascii; + System.out.println("Character: " + character); + } +} diff --git a/Lab2/Exercise_06_Max_Day_in_Month.java b/Lab2/Exercise_06_Max_Day_in_Month.java new file mode 100644 index 0000000..13d7ee9 --- /dev/null +++ b/Lab2/Exercise_06_Max_Day_in_Month.java @@ -0,0 +1,33 @@ +package Lab2; + +import java.util.Scanner; + +public class Exercise_06_Max_Day_in_Month { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter month (1-12): "); + int month = input.nextInt(); + System.out.print("Enter year: "); + int year = input.nextInt(); + + int days; + switch (month) { + case 1: case 3: case 5: case 7: case 8: case 10: case 12: + days = 31; + break; + case 4: case 6: case 9: case 11: + days = 30; + break; + case 2: + if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { + days = 29; + } else { + days = 28; + } + break; + default: + days = 0; // Invalid month + } + System.out.println("Days in month: " + days); + } +} diff --git a/Lab2/Exercise_07_Grade_Marks.java b/Lab2/Exercise_07_Grade_Marks.java new file mode 100644 index 0000000..d95a20d --- /dev/null +++ b/Lab2/Exercise_07_Grade_Marks.java @@ -0,0 +1,23 @@ +package Lab2; + +import java.util.Scanner; + +public class Exercise_07_Grade_Marks { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter your assignment marks: "); + int marks = input.nextInt(); + String grade; + + if (marks >= 0 && marks <= 40) grade = "F (Fail)"; + else if (marks <= 49) grade = "F+ (Marginal Fail)"; + else if (marks <= 54) grade = "D (Pass)"; + else if (marks <= 64) grade = "C"; + else if (marks <= 69) grade = "B (Credit)"; + else if (marks <= 74) grade = "B+"; + else if (marks <= 79) grade = "A (Distinction)"; + else grade = "A+"; + + System.out.println("Grade: " + grade); + } +} diff --git a/Lab2/Exercise_08_Sum_in_Double_Array.java b/Lab2/Exercise_08_Sum_in_Double_Array.java new file mode 100644 index 0000000..6338369 --- /dev/null +++ b/Lab2/Exercise_08_Sum_in_Double_Array.java @@ -0,0 +1,20 @@ +package Lab2; + +public class Exercise_08_Sum_in_Double_Array { + public static void main(String[] args) { + double[] numbers = new double[100]; + double sum = 0; + + // Initialize array with some values + for (int i = 0; i < numbers.length; i++) { + numbers[i] = i + 1; // Fill with values 1 to 100 + } + + // Enhanced for loop + for (double num : numbers) { + sum += num; + } + + System.out.println("Sum of all values: " + sum); + } +} \ No newline at end of file diff --git a/Lab2/Exercise_09_Compute_University_Tuition.java b/Lab2/Exercise_09_Compute_University_Tuition.java new file mode 100644 index 0000000..2550cb4 --- /dev/null +++ b/Lab2/Exercise_09_Compute_University_Tuition.java @@ -0,0 +1,14 @@ +package Lab2; + +public class Exercise_09_Compute_University_Tuition { + public static void main(String[] args) { + double tuition = 10000; + double rate = 0.05; + + for (int year = 1; year <= 10; year++) { + tuition += tuition * rate; + } + + System.out.println("Tuition after 10 years: RM" + tuition); + } +} diff --git a/Lab2/Exercise_10_Program_execution.java b/Lab2/Exercise_10_Program_execution.java new file mode 100644 index 0000000..54c8ecb --- /dev/null +++ b/Lab2/Exercise_10_Program_execution.java @@ -0,0 +1,19 @@ +package Lab2; + +import java.util.Scanner; + +public class Exercise_10_Program_execution { + + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + String choice; + + do { + System.out.println("Program is running..."); + System.out.print("Do you want to continue (Yes/No)? "); + choice = input.next(); + } while (choice.equalsIgnoreCase("Yes")); + + System.out.println("Program terminated."); + } +} \ No newline at end of file diff --git a/Lab2/Exercise_11_Investment_Value_Calculation.java b/Lab2/Exercise_11_Investment_Value_Calculation.java new file mode 100644 index 0000000..8677c50 --- /dev/null +++ b/Lab2/Exercise_11_Investment_Value_Calculation.java @@ -0,0 +1,18 @@ +package Lab2; + +import java.util.Scanner; + +public class Exercise_11_Investment_Value_Calculation { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.print("Enter investment amount: "); + double investmentAmount = input.nextDouble(); + System.out.print("Enter annual interest rate (in percentage): "); + double annualInterestRate = input.nextDouble(); + System.out.print("Enter number of years: "); + int years = input.nextInt(); + + double futureInvestmentValue = investmentAmount * Math.pow((1 + annualInterestRate / 100), years); + System.out.println("Future investment value: " + futureInvestmentValue); + } +} diff --git a/Lab2/Exercise_13_Sum_of_Prime_Num_First100.java b/Lab2/Exercise_13_Sum_of_Prime_Num_First100.java new file mode 100644 index 0000000..b3545c8 --- /dev/null +++ b/Lab2/Exercise_13_Sum_of_Prime_Num_First100.java @@ -0,0 +1,21 @@ +package Lab2; + +public class Exercise_13_Sum_of_Prime_Num_First100 { + public static void main(String[] args) { + int sum = 0; + for (int i = 2; i < 100; i++) { + if (isPrime(i)) { + sum += i; + } + } + System.out.println("Sum of prime numbers in the first 100 numbers: " + sum); + } + + public static boolean isPrime(int n) { + if (n <= 1) return false; + for (int i = 2; i <= Math.sqrt(n); i++) { + if (n % i == 0) return false; + } + return true; + } +}