diff --git a/FIRST-java-Q1.java b/FIRST-java-Q1.java new file mode 100644 index 000000000..c2c484aa0 --- /dev/null +++ b/FIRST-java-Q1.java @@ -0,0 +1,21 @@ +//Write a program to print whether a number is even or odd, also take +//input from the user. +import java.util.Scanner; +public class FIRST-java-Q1 +{ + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + System.out.print("Enter a number: "); + int num = sc.nextInt(); + + if (num % 2 == 0) { + System.out.println(num + " is an even number."); + } + else { + System.out.println(num + " is an odd number."); + } + + // Close the scanner to prevent resource leaks + sc.close(); +} diff --git a/FIRST_java_Q3.java b/FIRST_java_Q3.java new file mode 100644 index 000000000..75f4e1023 --- /dev/null +++ b/FIRST_java_Q3.java @@ -0,0 +1,29 @@ +// Write a program to input principal, time, and rate (P, T, R) from the user and +//find Simple Interest. + +import java.util.Scanner; + +public class FIRST_java_Q3 { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Input principal, time, and rate + System.out.print("Enter principal (P): "); + double principal = scanner.nextDouble(); + + System.out.print("Enter time in years (T): "); + double time = scanner.nextDouble(); + + System.out.print("Enter rate of interest (R): "); + double rate = scanner.nextDouble(); + + // Calculate Simple Interest + double simpleInterest = (principal * time * rate) / 100; + + // Output the result + System.out.printf("Simple Interest is: %.2f%n", simpleInterest); + + // Close the scanner + scanner.close(); + } +} \ No newline at end of file diff --git a/FIRST_java_Q4.java b/FIRST_java_Q4.java new file mode 100644 index 000000000..f45a5da69 --- /dev/null +++ b/FIRST_java_Q4.java @@ -0,0 +1,53 @@ + //Take in two numbers and an operator (+, -, *, /) and calculate the value. +//(Use if conditions) +import java.util.Scanner; + +public class FIRST_java_Q4 { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Input first number + System.out.print("Enter first number: "); + double num1 = scanner.nextDouble(); + + // Input second number + System.out.print("Enter second number: "); + double num2 = scanner.nextDouble(); + + // Input operator + System.out.print("Enter operator (+, -, *, /): "); + char operator = scanner.next().charAt(0); + + double result; + + // Perform calculation based on the operator + if (operator == '+') { + result = num1 + num2; + } + else if (operator == '-') { + result = num1 - num2; + } + else if (operator == '*') { + result = num1 * num2; + } + else if (operator == '/') { + if (num2 != 0) { + result = num1 / num2; + } else { + System.out.println("Error: Division by zero is not allowed."); + scanner.close(); + return; + } + } else { + System.out.println("Error: Invalid operator."); + scanner.close(); + return; + } + + // Output the result + System.out.printf("Result: %.2f%n", result); + + // Close the scanner + scanner.close(); + } +} diff --git a/FIRSTjava-Q2.java b/FIRSTjava-Q2.java new file mode 100644 index 000000000..5b544aeb0 --- /dev/null +++ b/FIRSTjava-Q2.java @@ -0,0 +1,14 @@ +// Take name as input and print a greeting message for that particular name. +class Q2 +{ + public static void main(String[] args) { + java.util.Scanner sc = new java.util.Scanner(System.in); + System.out.print("Enter your name: "); + String name = sc.nextLine(); + + System.out.println("Hello, " + name + "! Welcome to the program."); + + // Close the scanner to prevent resource leaks + sc.close(); + } +} diff --git a/FLOW_Q3_Krish.java b/FLOW_Q3_Krish.java new file mode 100644 index 000000000..cc3bbe5c9 --- /dev/null +++ b/FLOW_Q3_Krish.java @@ -0,0 +1,18 @@ +// Take a number as input and print the multiplication table for it. +class FLOW_Q3_Krish +{ + public static void main(String[] args) + { + java.util.Scanner sc = new java.util.Scanner(System.in); + System.out.print("Enter a number to print its multiplication table: "); + int num = sc.nextInt(); + + System.out.println("Multiplication table for " + num + ":"); + for (int i = 1; i <= 10; i++) { + System.out.println(num + " x " + i + " = " + (num * i)); + } + // + // Close the scanner to prevent resource leaks + sc.close(); + } +} diff --git a/FLOW_Q4_Krish.java b/FLOW_Q4_Krish.java new file mode 100644 index 000000000..42806b121 --- /dev/null +++ b/FLOW_Q4_Krish.java @@ -0,0 +1,30 @@ +// Take 2 numbers as inputs and find their HCF and LCM. +public class FLOW_Q4_Krish +{ + public static void main(String[] args) + { + java.util.Scanner sc = new java.util.Scanner(System.in); + System.out.print("Enter first number: "); + int num1 = sc.nextInt(); + System.out.print("Enter second number: "); + int num2 = sc.nextInt(); + + int hcf = findHCF(num1, num2); + int lcm = (num1 * num2) / hcf; + + System.out.println("HCF of " + num1 + " and " + num2 + " is: " + hcf); + System.out.println("LCM of " + num1 + " and " + num2 + " is: " + lcm); + + // Close the scanner to prevent resource leaks + sc.close(); + } + + private static int findHCF(int a, int b) { + while (b != 0) { + int temp = b; + b = a % b; + a = temp; + } + return a; + } +} diff --git a/FLOW_Q5_Krish.java b/FLOW_Q5_Krish.java new file mode 100644 index 000000000..c6a8d37a6 --- /dev/null +++ b/FLOW_Q5_Krish.java @@ -0,0 +1,29 @@ +//Keep taking numbers as inputs till the user enters ‘x’, after that print sum of all. +public class FLOW_Q5_Krish +{ + public static void main(String[] args) + { + java.util.Scanner sc = new java.util.Scanner(System.in); + int sum = 0; + String input; + + System.out.println("Enter numbers to sum them up. Type 'x' to finish:"); + while (true) { + input = sc.nextLine(); + if (input.equalsIgnoreCase("x")) { + break; + } + try { + int num = Integer.parseInt(input); + sum += num; + } catch (NumberFormatException e) { + System.out.println("Invalid input, please enter a number or 'x' to exit."); + } + } + + System.out.println("The total sum is: " + sum); + + // Close the scanner to prevent resource leaks + sc.close(); + } +} diff --git a/FLow_Q2_Krish .java b/FLow_Q2_Krish .java new file mode 100644 index 000000000..976b0800a --- /dev/null +++ b/FLow_Q2_Krish .java @@ -0,0 +1,18 @@ +// Take two numbers and print the sum of both. +import java.util.Scanner; +class FLow_Q2_Krish +{ + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + System.out.print("Enter first number: "); + int num1 = sc.nextInt(); + System.out.print("Enter second number: "); + int num2 = sc.nextInt(); + + int sum = num1 + num2; + System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum); + // Close the scanner to prevent resource leaks + sc.close(); + } +} diff --git a/lectures/20-trees/code/AVL/.project b/lectures/20-trees/code/AVL/.project index a2380fe36..b545323bf 100644 --- a/lectures/20-trees/code/AVL/.project +++ b/lectures/20-trees/code/AVL/.project @@ -22,12 +22,12 @@ - 1679078569810 + 1752678436405 30 org.eclipse.core.resources.regexFilterMatcher - node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ diff --git a/lectures/20-trees/code/Questions/.project b/lectures/20-trees/code/Questions/.project index a2380fe36..a73354206 100644 --- a/lectures/20-trees/code/Questions/.project +++ b/lectures/20-trees/code/Questions/.project @@ -22,12 +22,12 @@ - 1679078569810 + 1752678436432 30 org.eclipse.core.resources.regexFilterMatcher - node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__