diff --git a/Code/BinarySearch.py b/Code/BinarySearch.py new file mode 100644 index 0000000..9922b4b --- /dev/null +++ b/Code/BinarySearch.py @@ -0,0 +1,34 @@ +def BinarySearch(array, low, high, find): + + while low <= high: + + mid = low + (high - low)//2; + + + if array[mid] == find: + return mid + + + elif array[mid] < find: + low = mid + 1 + + + else: + high = mid - 1 + + # If the element is not present it returns -1 + return -1 + + +# Sample array +arr = [ 2, 3, 4, 10, 40 ] +find = 2 + + +found = BinarySearch(arr, 0, len(arr)-1, find) + +if found != -1: + print ("Given element %d is found at index %d" %(find, found)) +else: + print ("Given element is not present in the given array") + diff --git a/Code/GCD.py b/Code/GCD.py new file mode 100644 index 0000000..19197fc --- /dev/null +++ b/Code/GCD.py @@ -0,0 +1,8 @@ +d1=int(raw_input("Enter first number:")) +d2=int(raw_input("Enter second number")) +rem=d1%d2 +while rem!=0 : + d1=d2 + d2=rem + rem=d1%d2 +print "gcd of given numbers is : %d" %(d2) diff --git a/Code/HCF.py b/Code/HCF.py new file mode 100644 index 0000000..88b1834 --- /dev/null +++ b/Code/HCF.py @@ -0,0 +1,16 @@ +# Program to find HCF in python + +def hcf(x, y): # x and y are two integers you can input + # smaller will be used to store the variable for running the shorter loop + ans = 1 + if (x < y): + smaller = x + else: + smaller = y + for i in range(2, smaller+1): + if ((x%i == 0) and (y%i == 0)): + ans = i + return ans + +print(hcf(27, 6)) + diff --git a/Code/bubbleSort.py b/Code/bubbleSort.py new file mode 100644 index 0000000..1382633 --- /dev/null +++ b/Code/bubbleSort.py @@ -0,0 +1,25 @@ +# Python program for implementation of Bubble Sort + +def bubbleSort(arr): + n = len(arr) + + # Traverse through all array elements + for i in range(n): + + # Last i elements are already in place + for j in range(0, n-i-1): + + # traverse the array from 0 to n-i-1 + # Swap if the element found is greater + # than the next element + if arr[j] > arr[j+1] : + arr[j], arr[j+1] = arr[j+1], arr[j] + +# Driver code to test above +arr = [64, 34, 25, 12, 22, 11, 90] + +bubbleSort(arr) + +print ("Sorted array is:") +for i in range(len(arr)): + print ("%d" %arr[i]), diff --git a/Code/calculator.py b/Code/calculator.py new file mode 100644 index 0000000..77860bf --- /dev/null +++ b/Code/calculator.py @@ -0,0 +1,41 @@ +# This function adds two numbers +def add(x, y): + return x + y + +# This function subtracts two numbers +def subtract(x, y): + return x - y + +# This function multiplies two numbers +def multiply(x, y): + return x * y + +# This function divides two numbers +def divide(x, y): + return x / y + +print("Select operation.") +print("1.Add") +print("2.Subtract") +print("3.Multiply") +print("4.Divide") + +# Take input from the user +choice = input("Enter choice(1/2/3/4):") + +num1 = int(input("Enter first number: ")) +num2 = int(input("Enter second number: ")) + +if choice == '1': + print(num1,"+",num2,"=", add(num1,num2)) + +elif choice == '2': + print(num1,"-",num2,"=", subtract(num1,num2)) + +elif choice == '3': + print(num1,"*",num2,"=", multiply(num1,num2)) + +elif choice == '4': + print(num1,"/",num2,"=", divide(num1,num2)) +else: + print("Invalid input") diff --git a/Code/divide.py b/Code/divide.py index 38b410a..b4ea620 100644 --- a/Code/divide.py +++ b/Code/divide.py @@ -1,4 +1,14 @@ -def divide(a, b): - return a / b +num1 = input('Enter first number: ') +num2 = input('Enter second number: ') + + + +if num2==0: + print 'Denominator cannot be 0' + +else: + Division=float(num1)/float(num2) + print Division + + -print divide(20, 2) diff --git a/Code/division.py b/Code/division.py new file mode 100644 index 0000000..65780fd --- /dev/null +++ b/Code/division.py @@ -0,0 +1,8 @@ + +firstnum = int(input("First number: ")) +secondnum = int(input("Second number: ")) + +if secondnum == 0: + print("Division by zero illegal.") +else: + print("The result is " + str(firstnum/secondnum)) \ No newline at end of file diff --git a/Code/factorial.py b/Code/factorial.py index 1040ccb..f0f7ef0 100644 --- a/Code/factorial.py +++ b/Code/factorial.py @@ -1,16 +1,16 @@ -def get_factorial(number): - answer = 1 # We start with 1 as usual for finding factorial - - for i in range(1, (number+1)): # 1 is added to second argument of range function since it in this syntax loop terminates at the second value without executing for that value. - answer = answer * i - return answer # answer now contains the calculated value +#program to find the factorial of a number provided by a user +if __name__ == "__main__": + number = int(raw_input("Enter a number to get its factorial: ")) + factorial = 1 - -if __name__ == '__main__': - number = input("Enter the number : ") - number = int(number) - factorial = get_factorial(number) - print ("The factorial of " + number+ " is = " + factorial) - + if number < 0: + print("Sorry, factorial doesn't exist for negative numbers!") + elif number == 0: + print("The factorial of 0 is 1") + else: + for i in range(1, number + 1): + factorial = factorial * i + print "The factorial of %r is %r" % (number, factorial) + \ No newline at end of file diff --git a/Code/fibonacci.py b/Code/fibonacci.py new file mode 100644 index 0000000..dbb8d91 --- /dev/null +++ b/Code/fibonacci.py @@ -0,0 +1,10 @@ +def fibonacci(n): + if(n <= 1): + return n + else: + return(fibonacci(n-1) + fibonacci(n-2)) +n = int(input("Enter number of terms:")) +print("Fibonacci sequence:") +for i in range(n): + print(fibonacci(i)) + diff --git a/Code/max2.py b/Code/max2.py new file mode 100644 index 0000000..dcd449b --- /dev/null +++ b/Code/max2.py @@ -0,0 +1,9 @@ +#Program to find maximum of 2 numbers +while 1: + n,m=map(int, raw_input()) + k = max(m,n) + print k + print "Do you want to continue(yes/no): " + s=raw_input() + if s=="no": + break diff --git a/Code/palindrome.py b/Code/palindrome.py new file mode 100644 index 0000000..32301c2 --- /dev/null +++ b/Code/palindrome.py @@ -0,0 +1,22 @@ +# function which return reverse of a string +def reverse(s): + return s[::-1] + +def isPalindrome(s): + # Calling reverse function + rev = reverse(s) + + # Checking if both string are equal or not + if (s == rev): + return True + return False + + +# Driver code +s = raw_input("Enter the string:") +ans = isPalindrome(s) + +if ans == 1: + print("Yes") +else: + print("No") diff --git a/Code/primeNumber.py b/Code/primeNumber.py new file mode 100644 index 0000000..e387c31 --- /dev/null +++ b/Code/primeNumber.py @@ -0,0 +1,22 @@ +# Python program to check if the input number is prime or not + +num = 407 + +# take input from the user +# num = int(input("Enter a number: ")) + +# prime numbers are greater than 1 +if num > 1: + # check for factors + for i in range(2,num): + if (num % i) == 0: + print(num,"is not a prime number") + print(i,"times",num//i,"is",num) + break + else: + print(num,"is a prime number") + +# if input number is less than +# or equal to 1, it is not prime +else: + print(num,"is not a prime number") diff --git a/Code/randomNumber.py b/Code/randomNumber.py new file mode 100644 index 0000000..4dfeb5c --- /dev/null +++ b/Code/randomNumber.py @@ -0,0 +1,6 @@ +# Program to generate a random number between 0 and 9 + +# import the random module +import random + +print(random.randint(0,9))