From 20c336c337c03955ffae20299177ccf44dcac8ed Mon Sep 17 00:00:00 2001 From: venkataveeresh Date: Sat, 28 Jun 2025 00:35:05 +0530 Subject: [PATCH] upadting the file --- binary_search.java | 66 +++++++++++++------------- calculator.java | 113 +++++++++++++++++++++++---------------------- 2 files changed, 92 insertions(+), 87 deletions(-) diff --git a/binary_search.java b/binary_search.java index e7069670..fbebca0c 100644 --- a/binary_search.java +++ b/binary_search.java @@ -1,40 +1,40 @@ -public class BinarySearch { - public static int binarySearch(int[] arr, int target) { - int left = 0; - int right = arr.length - 1; - - while (left <= right) { - int mid = left + (right - left) / 2; - - // If the target is found at the middle index, return it. - if (arr[mid] == target) { - return mid; - } - - // If the target is smaller, search the left half. - if (arr[mid] > target) { - right = mid - 1; - } - // If the target is larger, search the right half. - else { - left = mid + 1; - } - } +public class Main{ - // If the target element is not found, return -1. - return -1; - } + public static int binarySearch(int array[], int left, int right, int item){ + + if (right >= left){ - public static void main(String[] args) { - int[] sortedArray = {1, 3, 5, 7, 9, 11, 13, 15}; - int targetElement = 7; + // calculation of new mid + int mid = left + (right - left)/2; - int result = binarySearch(sortedArray, targetElement); + // returns position where found + if (array[mid] == item) + return mid+1; - if (result != -1) { - System.out.println("Element " + targetElement + " found at index " + result + "."); - } else { - System.out.println("Element " + targetElement + " not found in the array."); + // goes to recursive calls in left half + if (array[mid] > item) + return binarySearch(array, left, mid-1, item); + + // goes to recursive calls in right half + else + return binarySearch(array, mid+1, right, item); } + // if element is not found we return -1 + else + return -1; + } + public static void main(String args[]){ + + int[ ] array = {10, 20, 30, 40, 50, 60, 70, 80}; + int item = 70; + int size = array.length; + + int position = binarySearch(array, 0, size-1, item); + + if(position == -1) + System.out.println("Element not found"); + else + System.out.println("The value " + item + " found at position: " + position); + } } diff --git a/calculator.java b/calculator.java index a1ad6216..f5d22d55 100644 --- a/calculator.java +++ b/calculator.java @@ -1,56 +1,61 @@ +// Java program for simple calculator +import java.io.*; +import java.lang.*; +import java.lang.Math; import java.util.Scanner; -class Main { - public static void main(String[] args) { - - char operator; - Double number1, number2, result; - - // create an object of Scanner class - Scanner input = new Scanner(System.in); - - // ask users to enter operator - System.out.println("Choose an operator: +, -, *, or /"); - operator = input.next().charAt(0); - - // ask users to enter numbers - System.out.println("Enter first number"); - number1 = input.nextDouble(); - - System.out.println("Enter second number"); - number2 = input.nextDouble(); - - switch (operator) { - - // performs addition between numbers - case '+': - result = number1 + number2; - System.out.println(number1 + " + " + number2 + " = " + result); - break; - - // performs subtraction between numbers - case '-': - result = number1 - number2; - System.out.println(number1 + " - " + number2 + " = " + result); - break; - - // performs multiplication between numbers - case '*': - result = number1 * number2; - System.out.println(number1 + " * " + number2 + " = " + result); - break; - - // performs division between numbers - case '/': - result = number1 / number2; - System.out.println(number1 + " / " + number2 + " = " + result); - break; - - default: - System.out.println("Invalid operator!"); - break; - } - - input.close(); - } -} \ No newline at end of file +// Driver class +public class BasicCalculator { + // main function + public static void main(String[] args) + { + // Stores two numbers + double num1, num2; + + // Take input from the user + Scanner sc = new Scanner(System.in); + + System.out.println("Enter the numbers:"); + + // Take the inputs + num1 = sc.nextDouble(); + num2 = sc.nextDouble(); + + System.out.println("Enter the operator (+,-,*,/):"); + + char op = sc.next().charAt(0); + double o = 0; + + switch (op) { + // case to add two numbers + case '+': + o = num1 + num2; + break; + + // case to subtract two numbers + case '-': + o = num1 - num2; + break; + + // case to multiply two numbers + case '*': + o = num1 * num2; + break; + + // case to divide two numbers + case '/': + o = num1 / num2; + break; + + default: + System.out.println("You enter wrong input"); + } + + System.out.println("The final result:"); + System.out.println(); + + // print the final result + System.out.println(num1 + " " + op + " " + num2 + + " = " + o); + } +}