From dbc2e6c65ee0c60e4ce9a4821715bb4b48286c39 Mon Sep 17 00:00:00 2001 From: Aayush Choudhary Date: Wed, 5 Feb 2025 03:48:29 +0530 Subject: [PATCH 1/2] Assignment submission for first java --- assignments/Assignment2firstjava.java | 131 ++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 assignments/Assignment2firstjava.java diff --git a/assignments/Assignment2firstjava.java b/assignments/Assignment2firstjava.java new file mode 100644 index 000000000..5dd5c7102 --- /dev/null +++ b/assignments/Assignment2firstjava.java @@ -0,0 +1,131 @@ +//Solution to all the code question +import java.util.*; +public class Assignment2firstjava { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + // 1. Even or Odd Check + System.out.print("Enter a number to check even or odd: "); + int num = sc.nextInt(); + if (num % 2 == 0) { + System.out.println(num + " is Even."); + } else { + System.out.println(num + " is Odd."); + } + + // 2. Greeting Message + System.out.print("Enter your name: "); + sc.nextLine(); // To consume the leftover newline + String name = sc.nextLine(); + System.out.println("Hello, " + name + "! Welcome to the Java program."); + + // 3. Simple Interest Calculation + System.out.print("Enter Principal (P): "); + double P = sc.nextDouble(); + System.out.print("Enter Time (T in years): "); + double T = sc.nextDouble(); + System.out.print("Enter Rate of Interest (R): "); + double R = sc.nextDouble(); + double simpleInterest = (P * T * R) / 100; + System.out.println("Simple Interest: " + simpleInterest); + + // 4. Basic Calculator + System.out.print("Enter first number: "); + double num1 = sc.nextDouble(); + System.out.print("Enter an operator (+, -, *, /): "); + char operator = sc.next().charAt(0); + System.out.print("Enter second number: "); + double num2 = sc.nextDouble(); + + if (operator == '+') { + System.out.println("Result: " + (num1 + num2)); + } else if (operator == '-') { + System.out.println("Result: " + (num1 - num2)); + } else if (operator == '*') { + System.out.println("Result: " + (num1 * num2)); + } else if (operator == '/') { + if (num2 != 0) + System.out.println("Result: " + (num1 / num2)); + else + System.out.println("Division by zero is not allowed."); + } else { + System.out.println("Invalid operator."); + } + + // 5. Largest Number + System.out.print("Enter first number: "); + int a = sc.nextInt(); + System.out.print("Enter second number: "); + int b = sc.nextInt(); + System.out.println("Largest number is: " + (a > b ? a : b)); + + // 6. Currency Conversion (INR to USD) + System.out.print("Enter amount in INR: "); + double inr = sc.nextDouble(); + double usd = inr / 82.5; // Assuming 1 USD = 82.5 INR + System.out.println("Equivalent USD: $" + usd); + + // 7. Fibonacci Series + System.out.print("Enter the number of terms for Fibonacci Series: "); + int n = sc.nextInt(); + System.out.println("Fibonacci Series up to " + n + " terms:"); + int first = 0, second = 1; + for (int i = 1; i <= n; i++) { + System.out.print(first + " "); + int next = first + second; + first = second; + second = next; + } + System.out.println(); + + // 8. Palindrome Check + System.out.print("Enter a string to check if it's a palindrome: "); + sc.nextLine(); // Consume leftover newline + String str = sc.nextLine(); + if (isPalindrome(str)) { + System.out.println(str + " is a Palindrome."); + } else { + System.out.println(str + " is NOT a Palindrome."); + } + + // 9. Armstrong Numbers Between Two Given Numbers + System.out.print("Enter the lower limit: "); + int low = sc.nextInt(); + System.out.print("Enter the upper limit: "); + int high = sc.nextInt(); + System.out.println("Armstrong numbers between " + low + " and " + high + ":"); + for (int i = low; i <= high; i++) { + if (isArmstrong(i)) { + System.out.print(i + " "); + } + } + System.out.println(); + + sc.close(); + } + + // Method to check if a string is a palindrome + public static boolean isPalindrome(String str) { + str = str.replaceAll("\\s+", "").toLowerCase(); // Remove spaces & lowercase + int left = 0, right = str.length() - 1; + while (left < right) { + if (str.charAt(left) != str.charAt(right)) { + return false; + } + left++; + right--; + } + return true; + } + + // Method to check if a number is an Armstrong number + public static boolean isArmstrong(int num) { + int original = num, sum = 0, digits = String.valueOf(num).length(); + while (num > 0) { + int digit = num % 10; + sum += Math.pow(digit, digits); + num /= 10; + } + return sum == original; + } +} From 6a999806f02f4d1c76a30c999847c190478d9e08 Mon Sep 17 00:00:00 2001 From: Aayush Choudhary Date: Wed, 5 Feb 2025 12:54:10 +0530 Subject: [PATCH 2/2] Add files via upload --- assignments/Assignment2.java | 185 +++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 assignments/Assignment2.java diff --git a/assignments/Assignment2.java b/assignments/Assignment2.java new file mode 100644 index 000000000..570c6f2f1 --- /dev/null +++ b/assignments/Assignment2.java @@ -0,0 +1,185 @@ +import java.util.Scanner; + +public class BasicDSA { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + // 1. Find Maximum Element in an Array + System.out.print("Enter array size: "); + int n = sc.nextInt(); + int[] arr = new int[n]; + + System.out.println("Enter array elements: "); + for (int i = 0; i < n; i++) { + arr[i] = sc.nextInt(); + } + + System.out.println("Maximum element: " + findMax(arr)); + System.out.println("Minimum element: " + findMin(arr)); + + // 2. Reverse an Array + System.out.println("Reversed array: "); + reverseArray(arr); + printArray(arr); + + // 3. Check if the Array is Sorted + System.out.println("Is the array sorted? " + isSorted(arr)); + + // 4. Find Second Largest Element + System.out.println("Second largest element: " + findSecondLargest(arr)); + + // 5. Check if a Number is Prime + System.out.print("Enter a number to check if prime: "); + int num = sc.nextInt(); + System.out.println(num + " is " + (isPrime(num) ? "Prime" : "Not Prime")); + + // 6. Find Factorial of a Number + System.out.print("Enter a number to find factorial: "); + int factNum = sc.nextInt(); + System.out.println("Factorial of " + factNum + " is " + factorial(factNum)); + + // 7. Find GCD of Two Numbers + System.out.print("Enter two numbers to find GCD: "); + int a = sc.nextInt(); + int b = sc.nextInt(); + System.out.println("GCD of " + a + " and " + b + " is " + gcd(a, b)); + + // 8. Binary Search on a Sorted Array + System.out.print("Enter element to search using Binary Search: "); + int key = sc.nextInt(); + System.out.println("Element found at index: " + binarySearch(arr, key)); + + // 9. Bubble Sort Algorithm + System.out.println("Sorted Array using Bubble Sort: "); + bubbleSort(arr); + printArray(arr); + + sc.close(); + } + + // Method to find the maximum element in an array + public static int findMax(int[] arr) { + int max = arr[0]; + for (int num : arr) { + if (num > max) { + max = num; + } + } + return max; + } + + // Method to find the minimum element in an array + public static int findMin(int[] arr) { + int min = arr[0]; + for (int num : arr) { + if (num < min) { + min = num; + } + } + return min; + } + + // Method to reverse an array + public static void reverseArray(int[] arr) { + int left = 0, right = arr.length - 1; + while (left < right) { + int temp = arr[left]; + arr[left] = arr[right]; + arr[right] = temp; + left++; + right--; + } + } + + // Method to check if an array is sorted + public static boolean isSorted(int[] arr) { + for (int i = 1; i < arr.length; i++) { + if (arr[i] < arr[i - 1]) { + return false; + } + } + return true; + } + + // Method to find the second largest element in an array + public static int findSecondLargest(int[] arr) { + int max = Integer.MIN_VALUE, secondMax = Integer.MIN_VALUE; + for (int num : arr) { + if (num > max) { + secondMax = max; + max = num; + } else if (num > secondMax && num != max) { + secondMax = num; + } + } + return secondMax; + } + + // Method to check if a number is prime + public static boolean isPrime(int num) { + if (num < 2) return false; + for (int i = 2; i <= Math.sqrt(num); i++) { + if (num % i == 0) return false; + } + return true; + } + + // Method to find factorial of a number + public static long factorial(int num) { + long fact = 1; + for (int i = 2; i <= num; i++) { + fact *= i; + } + return fact; + } + + // Method to find GCD using Euclidean Algorithm + public static int gcd(int a, int b) { + while (b != 0) { + int temp = b; + b = a % b; + a = temp; + } + return a; + } + + // Method for Binary Search + public static int binarySearch(int[] arr, int key) { + int left = 0, right = arr.length - 1; + while (left <= right) { + int mid = left + (right - left) / 2; + if (arr[mid] == key) return mid; + if (arr[mid] < key) left = mid + 1; + else right = mid - 1; + } + return -1; // Element not found + } + + // Method for Bubble Sort + public static void bubbleSort(int[] arr) { + int n = arr.length; + boolean swapped; + for (int i = 0; i < n - 1; i++) { + swapped = false; + for (int j = 0; j < n - 1 - i; j++) { + if (arr[j] > arr[j + 1]) { + // Swap + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + swapped = true; + } + } + // If no two elements were swapped, the array is sorted + if (!swapped) break; + } + } + + // Helper Method to Print an Array + public static void printArray(int[] arr) { + for (int num : arr) { + System.out.print(num + " "); + } + System.out.println(); + } +}