diff --git a/Lab Assignment 11 (1)-checkpoint.ipynb b/Lab Assignment 11 (1)-checkpoint.ipynb new file mode 100644 index 0000000..10be757 --- /dev/null +++ b/Lab Assignment 11 (1)-checkpoint.ipynb @@ -0,0 +1,723 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Python function \n", + "## Every solution should be done with the help of functions." + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Function is used for modularity in the program.\n", + "\n", + "def function_name(argument-1, argument-2,argument-3...):\n", + " function body\n", + " .\n", + " .\n", + " .\n", + " .\n", + " .\n", + " return var \n", + "#function call\n", + "function_name(10,20,30)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 1. Write a program to compute gcd of two user input numbers.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the value of x = 45\n", + "Enter the value of y = 90\n", + "GCD = 45\n", + "GCD 2 = 45\n" + ] + } + ], + "source": [ + "def gcd(a,b):\n", + " for i in range(b,2,-1):\n", + " if(a%i==0 and b%i==0):\n", + " return i\n", + "f=0\n", + "x=int(input(\"Enter the value of x = \"))\n", + "y=int(input(\"Enter the value of y = \"))\n", + "if(x>y):\n", + " f=gcd(x,y)\n", + "else:\n", + " f=gcd(y,x)\n", + "print(\"GCD = \",f)\n", + "print(\"GCD 2 = \",gcd(x,y))\n", + "\n", + "def gcd2(a,b):\n", + " while(b!=0):\n", + " x,y=y,x%y\n", + " return x" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 2.Write a program to take an integer from the user and return reverse of the number.\n", + "### (Example: I/P 123, O/P: 321) " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter a number = 123\n", + "Reversed Number = 321\n" + ] + } + ], + "source": [ + "def rev(a):\n", + " l=0\n", + " res=0\n", + " while(a!=0):\n", + " l=a%10\n", + " a=a//10\n", + " res=res*10+l\n", + " return res\n", + "x=int(input(\"Enter a number = \"))\n", + "x=rev(x)\n", + "print(\"Reversed Number = \",x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 3. Write a program in Python to print prime numbers between 25 and 100 using function." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Prime numbers from 25 to 100 = \n", + "29\n", + "31\n", + "37\n", + "41\n", + "43\n", + "47\n", + "53\n", + "59\n", + "61\n", + "67\n", + "71\n", + "73\n", + "79\n", + "83\n", + "89\n", + "97\n" + ] + } + ], + "source": [ + "def prime(a):\n", + " for i in range(2,a):\n", + " if(a%i==0):\n", + " return False\n", + " return True\n", + "print(\"Prime numbers from 25 to 100 = \")\n", + "for i in range(25,101):\n", + " if(prime(i)):\n", + " print(i)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 4. Define a Pyhton function pow1 that takes the values of two integers m and n from the user and calculate pow(m,n). Call this function with different values.\n", + "### Do not use exponent operator (**) or pow()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the number whose power is to be calculated= 5\n", + "Enter the power = 2\n", + "Result = 25\n" + ] + } + ], + "source": [ + "\n", + "def power(m,n):\n", + " if(n>0):\n", + " return m*power(m,n-1)\n", + " else:\n", + " return 1\n", + "a=0\n", + "x=int(input(\"Enter the number whose power is to be calculated= \"))\n", + "y=int(input(\"Enter the power = \"))\n", + "a=power(x,y)\n", + "print(\"Result = \",a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 5. Write a Python program to find the factorial of a number using a recursive function." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the number whose factorial is to be calculated= 9\n", + "Result = 362880\n" + ] + } + ], + "source": [ + "def fact(m):\n", + " if(m>0):\n", + " return m*fact(m-1)\n", + " else:\n", + " return 1\n", + "a=0\n", + "x=int(input(\"Enter the number whose factorial is to be calculated= \"))\n", + "a=fact(x)\n", + "print(\"Result = \",a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 6. Design and develop a python function RightShift(x ,n) that takes two integers x and n as input and returns value of the integer x shifted to the right by n positions. Assume the integers are unsigned. \n", + "### Write a Python program that invokes this function with multiple values for x and n and tabulate the results with suitable headings withour using right shift operator." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the integer x = 60\n", + "Enter the integer n = 3\n", + "111100\n", + "111\n", + "Enter 1 to continue and any other number to exit2\n", + "Program Ended\n" + ] + } + ], + "source": [ + "def RightShift(x,n):\n", + " while(n>0):\n", + " x=x//10\n", + " n=n-1\n", + " return(x)\n", + "def inp(): \n", + " x=int(input(\"Enter the integer x = \"))\n", + " n=int(input(\"Enter the integer n = \"))\n", + " a=0\n", + " res=0\n", + " t=0\n", + " copy=x\n", + " while(x!=0):\n", + " a=(x%2)*10**t\n", + " x=x//2\n", + " res=res+a\n", + " t+=1\n", + " print(res)\n", + " res=RightShift(res,n)\n", + " print(res)\n", + "inp()\n", + "while(True):\n", + " a= int(input(\"Enter 1 to continue and any other number to exit\"))\n", + " if(a==1):\n", + " inp()\n", + " else:\n", + " break\n", + "print(\"Program Ended\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 7. Write a program in python to find the sum of the series 1!/1+2!/2+3!/3+4!/4+5!/5 using the function. " + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the value of n = 6\n", + "Sum = 154.0\n" + ] + } + ], + "source": [ + "def fact(m):\n", + " if(m>0):\n", + " return m*fact(m-1)\n", + " else:\n", + " return 1\n", + "sum=0\n", + "n =int(input('Enter the value of n = '))\n", + "for i in range(1,n+1):\n", + " sum=sum+(fact(i)/i)\n", + "print(\"Sum = \",sum)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 8. Define a function to print nth term in Fibonacci series using recursion. Call this function with different values." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the term to be printed = 6\n", + "5\n", + "Enter 1 to continue and any other number to exit1\n", + "Enter the term to be printed = 12\n", + "89\n", + "Enter 1 to continue and any other number to exit2\n", + "Program Ended\n" + ] + } + ], + "source": [ + "def Fibo(nth):\n", + " a=0\n", + " b=1\n", + " c=0\n", + " if(nth==1):\n", + " return a\n", + " elif(nth==2):\n", + " return b\n", + " for i in range (2,nth):\n", + " c=a+b\n", + " a=b\n", + " b=c\n", + " return c\n", + "\n", + "n=int(input(\"Enter the term to be printed = \"))\n", + "b=Fibo(n)\n", + "print(b)\n", + "\n", + "def inp():\n", + " n=int(input(\"Enter the term to be printed = \"))\n", + " b=Fibo(n)\n", + " print(b)\n", + "\n", + "while(True):\n", + " f= int(input(\"Enter 1 to continue and any other number to exit\"))\n", + " if(f==1):\n", + " inp()\n", + " else:\n", + " break\n", + "print(\"Program Ended\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 9. Define a python function to convert a decimal number to its equivalent octal number without using inbuilt functions. Call this function with different values." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the term to be printed = 45\n", + "55\n", + "Enter 1 to continue and any other number to exit1\n", + "Enter the term to be printed = 456\n", + "710\n", + "Enter 1 to continue and any other number to exit0\n", + "Program Ended\n" + ] + } + ], + "source": [ + "def octa(p):\n", + " rem=0\n", + " res=0\n", + " t=0\n", + " while(p!=0):\n", + " rem=(p%8)*10**t\n", + " res=res+rem\n", + " p=p//8\n", + " t=t+1\n", + " return res\n", + "n=int(input(\"Enter the term to be printed = \"))\n", + "b=octa(n)\n", + "print(b)\n", + "\n", + "def inp():\n", + " n=int(input(\"Enter the term to be printed = \"))\n", + " b=octa(n)\n", + " print(b)\n", + "\n", + "while(True):\n", + " f= int(input(\"Enter 1 to continue and any other number to exit\"))\n", + " if(f==1):\n", + " inp()\n", + " else:\n", + " break\n", + "print(\"Program Ended\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 10. Write a python function to Check whether a number can be expressed as the sum of two prime number. For example, 20 can be written as sum of 7 and 13. 29 cannot be written as sum of 2 prime numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the number to be checked = 29\n", + "29 cannot be written as sum of 2 primes \n" + ] + } + ], + "source": [ + "def Prime(n):\n", + " for i in range(2,n):\n", + " if(n%i==0):\n", + " return(False)\n", + " return(True)\n", + "n=int(input(\"Enter the number to be checked = \"))\n", + "copy=n\n", + "flag=0\n", + "for i in range (2,n//2+1):\n", + " if(Prime(i) and Prime(copy-i)):\n", + " flag=1\n", + " print(\"Thr number can be represented as sum of = \",i,(copy-i))\n", + "if(flag==0):\n", + " print(n,\"cannot be written as sum of 2 primes \")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Optional Questions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 11. Write two python function to Convert octal Number to decimal without using inbuilt functions." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the octal number = 654\n", + "Decimal Number = 428\n" + ] + } + ], + "source": [ + "n=int(input(\"Enter the octal number = \"))\n", + "t=0\n", + "dec=0\n", + "while(n>0):\n", + " l=n%10\n", + " dec=dec+l*8**t\n", + " t=t+1\n", + " n=n//10\n", + "print(\"Decimal Number = \",dec) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 12. Write a program in Python to check whether two given strings are an anagram. \n", + "### An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are anagram of each other." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the first string = abcd\n", + "Enter the second string = bcda\n", + "They are anagrams.\n" + ] + } + ], + "source": [ + "a=input(\"Enter the first string = \")\n", + "b=input(\"Enter the second string = \")\n", + "g=0\n", + "for i in a:\n", + " flag=0\n", + " char=i\n", + " for j in b:\n", + " if(char==j):\n", + " flag=1\n", + " if(flag==0):\n", + " print(\"The inputed statements are not anagram\")\n", + " g=1\n", + " break\n", + "if(g==0):\n", + " print(\"They are anagrams.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 13. Write a python program that calculates the bill for a customer purchase in a store. in this store the item_id range from 1 to 100. These items are organised in a way that the items with item_id less than 50 are on sale (10 %) and item_id range from 50-80 are on sale (25 %) and the rest items are on the sale of 40 %.\n", + "### Your program has a function call (saleprice) that receives the itme_id, and its price, and it returns the current price after sale for current item, and it prints the number of items purchased so far.\n", + "### In your main, you should read item id and call saleprice function for each item. your program should continue to nread item_id till the end of purchase. entering 0 will indicate the end of purchase. At the end your program should print the total amount of bill. " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the item id = 45\n", + "Enter the term price of the item = 550\n", + "Sale price = 495.0\n", + "Enter 1 to continue and 0 to end the program450\n", + "Invalid Input\n", + "Enter 1 to continue and 0 to end the program1\n", + "Enter the item id = 50\n", + "Enter the term price of the item = 600\n", + "Sale price = 450.0\n", + "Enter 1 to continue and 0 to end the program1\n", + "Enter the item id = 80\n", + "Enter the term price of the item = 800\n", + "Sale price = 600.0\n", + "Enter 1 to continue and 0 to end the program1\n", + "Enter the item id = 90\n", + "Enter the term price of the item = 800\n", + "Sale price = 480.0\n", + "Enter 1 to continue and 0 to end the program0\n", + "Number of elements = 4\n", + "Program Ended\n" + ] + } + ], + "source": [ + "def saleprice(a,b):\n", + " if(a<50):\n", + " b=b-(10/100*b)\n", + " elif(a>=50 and a<=80):\n", + " b=b-(25/100*b)\n", + " else:\n", + " b=b-(40/100*b)\n", + " print(\"Sale price = \",b)\n", + "def inp(): \n", + " item_id=int(input(\"Enter the item id = \"))\n", + " n=int(input(\"Enter the term price of the item = \"))\n", + " saleprice(item_id,n)\n", + "inp()\n", + "flag=1\n", + "while(True):\n", + " f= int(input(\"Enter 1 to continue and 0 to end the program\"))\n", + " if(f==1):\n", + " flag=flag+1\n", + " inp()\n", + " elif(f==0):\n", + " break\n", + " else:\n", + " print(\"Invalid Input\")\n", + "print(\"Number of elements = \",flag)\n", + "print(\"Program Ended\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 14.We add a Leap Day on February 29, almost every four years. The leap day is an extra, or intercalary day and we add it to the shortest month of the year, February. \n", + "## In the Gregorian calendar three criteria must be taken into account to identify leap years:\n", + "\n", + "### The year can be evenly divided by 4, is a leap year, unless:\n", + "### The year can be evenly divided by 100, it is NOT a leap year, unless:\n", + "### The year is also evenly divisible by 400. Then it is a leap year.\n", + "### This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.\n", + "\n", + "## Sample Input\n", + "### 1990\n", + "\n", + "## Sample Output\n", + "### False\n", + "\n", + "## Sample Input\n", + "### 1600\n", + "\n", + "## Sample Output\n", + "### True" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the year = 2000\n", + "True\n", + "Enter 1 to continue and 0 to end the program5\n", + "Invalid Input\n", + "Enter 1 to continue and 0 to end the program1\n", + "Enter the year = 1990\n", + "False\n", + "Enter 1 to continue and 0 to end the program1\n", + "Enter the year = 1600\n", + "True\n", + "Enter 1 to continue and 0 to end the program0\n", + "Program Ended\n" + ] + } + ], + "source": [ + "\n", + "def check(a): \n", + " if(a%4==0 and a%100==0 and a%400==0):\n", + " return True\n", + " else:\n", + " return False\n", + "def inp():\n", + " n=int(input(\"Enter the year = \"))\n", + " if(check(n)):\n", + " print(\"True\")\n", + " else:\n", + " print(\"False\")\n", + "inp()\n", + "while(True):\n", + " f= int(input(\"Enter 1 to continue and 0 to end the program\"))\n", + " if(f==1):\n", + " inp()\n", + " elif(f==0):\n", + " break\n", + " else:\n", + " print(\"Invalid Input\")\n", + "print(\"Program Ended\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}