diff --git a/Fibonacci.py b/Fibonacci.py new file mode 100644 index 0000000..5c4a6b1 --- /dev/null +++ b/Fibonacci.py @@ -0,0 +1,28 @@ +# Program to display the Fibonacci sequence up to n-th term where n is provided by the user + +# change this value for a different result +#nterms = 10 + +# uncomment to take input from the user +nterms = int(input("How many terms? ")) + +# first two terms +n1 = 0 +n2 = 1 +count = 0 + +# check if the number of terms is valid +if nterms <= 0: + print("Please enter a positive integer") +elif nterms == 1: + print("Fibonacci sequence upto",nterms,":") + print(n1) +else: + print("Fibonacci sequence upto",nterms,":") + while count < nterms: + print(n1,end=' , ') + nth = n1 + n2 + # update values + n1 = n2 + n2 = nth + count += 1 diff --git a/factorial.py b/factorial.py new file mode 100644 index 0000000..41a6ca2 --- /dev/null +++ b/factorial.py @@ -0,0 +1,8 @@ +fact = 1 +n = int(input("Enter value:")) + +for i in range(1,n+1): + fact = fact * i + +print("Factorial of " + str(n) + " is " + str(fact)) + diff --git a/quick_sort.py b/quick_sort.py new file mode 100644 index 0000000..5ddf3fe --- /dev/null +++ b/quick_sort.py @@ -0,0 +1,19 @@ +def partition(arr,low,high): + i = ( low-1 ) + pivot = arr[high] + + for j in range(low , high): + if arr[j] <= pivot: + i = i+1 + arr[i],arr[j] = arr[j],arr[i] + + arr[i+1],arr[high] = arr[high],arr[i+1] + return ( i+1 ) + + +def quickSort(arr,low,high): + if low < high: + pi = partition(arr,low,high) + + quickSort(arr, low, pi-1) + quickSort(arr, pi+1, high) \ No newline at end of file