Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lab2/Excecise_12_Perfect_Num_First100000.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: ");
Expand Down
13 changes: 13 additions & 0 deletions Lab2/Exercise_04_Uppercase_Letter_to_Lowercase.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 13 additions & 0 deletions Lab2/Exercise_05_Character_to_ASCII.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
33 changes: 33 additions & 0 deletions Lab2/Exercise_06_Max_Day_in_Month.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
23 changes: 23 additions & 0 deletions Lab2/Exercise_07_Grade_Marks.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
20 changes: 20 additions & 0 deletions Lab2/Exercise_08_Sum_in_Double_Array.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
14 changes: 14 additions & 0 deletions Lab2/Exercise_09_Compute_University_Tuition.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
19 changes: 19 additions & 0 deletions Lab2/Exercise_10_Program_execution.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}
18 changes: 18 additions & 0 deletions Lab2/Exercise_11_Investment_Value_Calculation.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
21 changes: 21 additions & 0 deletions Lab2/Exercise_13_Sum_of_Prime_Num_First100.java
Original file line number Diff line number Diff line change
@@ -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;
}
}