Python Basic Problems - Ipynb
AI-enhanced title
"cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized useor distribution prohibited" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#check even odd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "num = int(input(\"Enter a number: \"))\n", "if (num % 2) == 0:\n", " print(num, \" is even\")\n", "else:\n", " print(num, \" is odd\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#check positive, negative or 0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "num = float(input(\"Enter a number: \"))\n", "if num > 0:\n", " print(\"Positive number\")\n", "elif num == 0:\n", " print(\"Zero\")\n", "else:\n", " print(\"Negative number\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Factorial of a number" ]},{ "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "factorial = 1\n", "# check if the number is negative, positive or zero\n", "if num < 0:\n", " print(\"Sorry, factorial does not exist for negative numbers\")\n", "elif num == 0:\n", " print(\"The factorial of 0 is 1\")\n", "else:\n", " for i in range(1,num + 1):\n", " factorial = factorial*i\n", " print(\"The factorial of\",num,\"is\",factorial)" ]},{ "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Reversing a number" ]},{ "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n=int(input(\"Enter number: \"))\n", "rev=0\n", "while(n>0):\n", " dig=n%10\n", " rev=rev*10+dig\n", " n=n//10\n", "print(\"Reverse of the number:\",rev)" ]},{ "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Check if it is a palindrome" ]},{ "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n=int(input(\"Enter number:\"))\n", "temp=n\n", "rev=0\n", "while(n>0):\n", " dig=n%10\n", " rev=rev*10+dig\n", " n=n//10\n", "if(temp==rev):\n", " print(\"The number is a palindrome!\")\n", "else:\n", " print(\"The number isn't a palindrome!\")" ]},{ "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#fibonacci - 0 1 1 2 3 5 8......." ]},{ "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter number:3\n", "1\n" ] } ], "source": [ "n=int(input(\"Enter number:\"))\n", "a = 0\n", "b = 1\n", "if n < 0: \n", " print(\"Incorrect input\") \n", "elif n == 0: \n", " print(a)\n", "elif n == 1: \n", " print(a) \n", "else: \n", " for i in range(2,n): \n", " c = a + b \n", " a = b \n", " b = c \n", " print(b) " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "1 \n", "\n", "2 2 \n", "\n", "3 3 3 \n", "\n", "4 4 4 4 \n", "\n", "5 5 5 5 5 \n", "\n" ] } ], "source": [ "# 10 is the total number to print\n", "for num in range(6):\n", " for i in range(num):\n", " print (num,end=\" \") #print number\n", " # new line after each row to display pattern correctly\n", " print(\"\\n\")" ] }, { "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2}