Skip to content

upadting the file #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
66 changes: 33 additions & 33 deletions binary_search.java
Original file line number Diff line number Diff line change
@@ -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);

}
}
113 changes: 59 additions & 54 deletions calculator.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
// 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);
}
}