diff --git a/lab11_E18CSE066_ISHAPUTHIGE (1).ipynb b/lab11_E18CSE066_ISHAPUTHIGE (1).ipynb new file mode 100644 index 0000000..f47308e --- /dev/null +++ b/lab11_E18CSE066_ISHAPUTHIGE (1).ipynb @@ -0,0 +1,509 @@ +{ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "enter no 11\n" + ] + } + ], + "source": [ + "\n", + "\n", + "def gcd(a,b):\n", + " if(a>b):\n", + " n=b\n", + " else:\n", + " n=a\n", + " for i in range(1,n+1):\n", + " if((a%i==0) and (b%i==0)):\n", + " g=i\n", + " return g\n", + "x=int(input('enter no 1'))\n", + "y=int(input('enter no 2'))\n", + "print(gcd(x,y))\n" + ] + }, + { + "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": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "enter no 1125\n", + "521\n" + ] + } + ], + "source": [ + "def rev(a):\n", + " r=0\n", + " while(a>0):\n", + " r=r*10 + (a%10)\n", + " a//=10\n", + " return r\n", + "x=int(input('enter no 1 '))\n", + "print(rev(x))\n", + " " + ] + }, + { + "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": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "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", + " flag=True\n", + " for i in range(2,a):\n", + " if(a%i==0):\n", + " flag=False\n", + " return flag\n", + "for i in range(25,101):\n", + " if(prime(i)== True):\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": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "enter no m5\n", + "enter no n3\n", + "125\n" + ] + } + ], + "source": [ + "def pow1(m,n):\n", + " prod=1\n", + " for i in range (1,n+1):\n", + " prod*=m\n", + " return prod\n", + "x=int(input('enter no m'))\n", + "y=int(input('enter no n'))\n", + "print(pow1(x,y))" + ] + }, + { + "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": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "enter no 15\n", + "120\n" + ] + } + ], + "source": [ + "def fact(n):\n", + " if n==0:\n", + " return 1\n", + " else:\n", + " return n*fact(n-1)\n", + "x=int(input('enter no 1'))\n", + "print(fact(x))" + ] + }, + { + "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": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "enter x2\n", + "enter n4\n", + "1\n", + "enter x-9\n", + "enter n3\n" + ] + } + ], + "source": [ + "def rightshift(a,b):\n", + " for i in range(1,b+1):\n", + " ans=a//2\n", + " return ans\n", + "while(True):\n", + " x=int(input('enter x'))\n", + " n=int(input('enter n'))\n", + " if(x>0):\n", + " print(rightshift(x,n))\n", + " else:\n", + " break" + ] + }, + { + "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": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "24.0\n" + ] + } + ], + "source": [ + "def fact(n):\n", + " if n==0:\n", + " return 1\n", + " else:\n", + " return n*fact(n-1)\n", + "for i in range(1,6):\n", + " sum=0\n", + " l=fact(i)\n", + " sum+=l/i\n", + "print(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": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "enter x5\n", + "3\n" + ] + } + ], + "source": [ + "def fib(n):\n", + " if n==1:\n", + " return 0\n", + " elif n==2:\n", + " return 1\n", + " else:\n", + " return fib(n-1)+fib(n-2)\n", + "x=int(input('enter x'))\n", + "print(fib(x))" + ] + }, + { + "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": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "enter x123\n", + "173\n" + ] + } + ], + "source": [ + "def convert(a):\n", + " l=''\n", + " while(a>0):\n", + " rem=a%8\n", + " a//=8\n", + " l+=str(rem)\n", + " return l\n", + "def rev(u):\n", + " a=int(convert(u))\n", + " r=0\n", + " while(a>0):\n", + " r=r*10 + (a%10)\n", + " a//=10\n", + " return r\n", + "x=int(input('enter x'))\n", + "print(rev(x))\n", + "\n" + ] + }, + { + "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": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "énter a no.19\n", + "it can be written\n" + ] + } + ], + "source": [ + "def prime(a):\n", + " flag=True\n", + " for i in range(2,a):\n", + " if(a%i==0):\n", + " flag=False\n", + " return flag\n", + "x=int(input('énter a no.'))\n", + "for i in range(1,x+1):\n", + " if(prime(i)==True):\n", + " if(prime(x-i)==True):\n", + " print('it can be written')\n", + " break\n", + " \n", + " " + ] + }, + { + "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": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "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": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "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": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "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": 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 +}