diff --git a/Python/program-14/git.py b/Python/program-14/git.py new file mode 100644 index 00000000..84546b60 --- /dev/null +++ b/Python/program-14/git.py @@ -0,0 +1,21 @@ +#fibonacci series +nterms = int(input("How many terms? ")) + +# first two terms +n1, n2 = 0, 1 +count = 0 + +if nterms <= 0: + print("Please enter a positive integer") +elif nterms == 1: + print("Fibonacci sequence upto",nterms,":") + print(n1) +else: + print("Fibonacci sequence:") + while count < nterms: + print(n1) + nth = n1 + n2 + # update values + n1 = n2 + n2 = nth + count += 1 \ No newline at end of file diff --git a/Python/program-14/readme.md b/Python/program-14/readme.md new file mode 100644 index 00000000..53622207 --- /dev/null +++ b/Python/program-14/readme.md @@ -0,0 +1 @@ +program to print fibonacci series diff --git a/Python/program-15/git1.py b/Python/program-15/git1.py new file mode 100644 index 00000000..9ce5c2ef --- /dev/null +++ b/Python/program-15/git1.py @@ -0,0 +1,22 @@ +# Program to check if a number is prime or not + +num = 407 + +# To 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") \ No newline at end of file diff --git a/Python/program-15/readme.md b/Python/program-15/readme.md new file mode 100644 index 00000000..a089d895 --- /dev/null +++ b/Python/program-15/readme.md @@ -0,0 +1 @@ +program to check whether a number is prime or not diff --git a/Python/program-16/git2.py b/Python/program-16/git2.py new file mode 100644 index 00000000..a6a74aa2 --- /dev/null +++ b/Python/program-16/git2.py @@ -0,0 +1,36 @@ +# Returns index of x in arr if present, else -1 +def binary_search(arr, low, high, x): + + # Check base case + if high >= low: + + mid = (high + low) // 2 + + # If element is present at the middle itself + if arr[mid] == x: + return mid + + # If element is smaller than mid, then it can only + # be present in left subarray + elif arr[mid] > x: + return binary_search(arr, low, mid - 1, x) + + # Else the element can only be present in right subarray + else: + return binary_search(arr, mid + 1, high, x) + + else: + # Element is not present in the array + return -1 + +# Test array +arr = [ 2, 3, 4, 10, 40 ] +x = 10 + +# Function call +result = binary_search(arr, 0, len(arr)-1, x) + +if result != -1: + print("Element is present at index", str(result)) +else: + print("Element is not present in array") \ No newline at end of file diff --git a/Python/program-16/readme.md b/Python/program-16/readme.md new file mode 100644 index 00000000..305d7c49 --- /dev/null +++ b/Python/program-16/readme.md @@ -0,0 +1 @@ +python program for binary search \ No newline at end of file diff --git a/Python/program-18/git3.py b/Python/program-18/git3.py new file mode 100644 index 00000000..cfbccef5 --- /dev/null +++ b/Python/program-18/git3.py @@ -0,0 +1,17 @@ +# Python3 program to find simple interest +# for given principal amount, time and +# rate of interest. + + +def simple_interest(p,t,r): + print('The principal is', p) + print('The time period is', t) + print('The rate of interest is',r) + + si = (p * t * r)/100 + + print('The Simple Interest is', si) + return si + +# Driver code +simple_interest(8, 6, 8) \ No newline at end of file diff --git a/Python/program-18/readme.md b/Python/program-18/readme.md new file mode 100644 index 00000000..0c0fcc1d --- /dev/null +++ b/Python/program-18/readme.md @@ -0,0 +1 @@ +python program for simple interest \ No newline at end of file