{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "41afbfda",
   "metadata": {},
   "source": [
    "# 1. Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bb10575",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = input(\"enter Name: \")\n",
    "age = int(input(\"enter your Age: \"))\n",
    "year_100 = 2025 + (100 - age)\n",
    "print(f\"{name}, you will turn 100 in {year_100}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b57cdea",
   "metadata": {},
   "source": [
    "# OR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acd5ffe6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "name=input(\"enter your name\")\n",
    "age=int(input(\"enter your age\"))\n",
    "\n",
    "current_year=datetime.now().year\n",
    "year_you_turn_100=current_year+(100-age)\n",
    "\n",
    "print(\"hello\",name,\" you will turn 100 in year\",year_you_turn_100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a55549a",
   "metadata": {},
   "source": [
    "# 2. Write a program to check whether the number is even or odd, print out an appropriate message to the user.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43c198ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "n=int(input(\"enter number \"))\n",
    "\n",
    "if(n%2==0):\n",
    "    print(n,\"number is even\")\n",
    "else:\n",
    "    print(n,\"number is odd\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ab5a0a0",
   "metadata": {},
   "source": [
    "# 3. Write program which will find all such numbers which are divisible by 7."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1508245",
   "metadata": {},
   "outputs": [],
   "source": [
    "p=int(input(\"enter starting number \"))\n",
    "q=int(input(\"enter end number \"))\n",
    "\n",
    "for n in range(p,q):\n",
    "    if n%7==0:\n",
    "        print(n,\"number is divisible by 7\")\n",
    "    else:\n",
    "        print(n,\"number is not divisible by 7\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce42e48e",
   "metadata": {},
   "source": [
    "# 4. Write a program which can compute the factorial of a given numbers.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67fd88e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "a=int(input(\"enter starting number \"))\n",
    "\n",
    "factorial=1\n",
    "for i in range(1,a+1):\n",
    "    factorial=i*factorial\n",
    "print(\"the factorial of\",a,\"is \" ,factorial)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66452eff",
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "num = int(input(\"Enter a number: \"))\n",
    "print(\"Factorial:\", math.factorial(num))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afc755af",
   "metadata": {},
   "source": [
    "# 5. Write program that prints out all the elements of list that are less than 10\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a0dfa25",
   "metadata": {},
   "outputs": [],
   "source": [
    "list=[1,2,3,4,5,6,7,8,9,4,5,6,5,55,5,55,66,44]\n",
    "for i in list:\n",
    "    if(i<10):\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "259f6dbc",
   "metadata": {},
   "source": [
    "# 6. Write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "61479e5e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "list1 = [1, 2, 3, 4, 5, 6]  \n",
    "list2 = [4, 5, 6, 7, 8, 9]  \n",
    "common = list(set(list1) & set(list2))  \n",
    "print(common)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "941938a1",
   "metadata": {},
   "source": [
    "# 7. To determine whether the number is prime or not."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9f9834db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "enter a number4\n",
      "4 is not prime\n"
     ]
    }
   ],
   "source": [
    "n= int(input(\"enter a number\"))\n",
    "cnt=0\n",
    "\n",
    "for i in range(2,n+1):\n",
    "    if(n%i==0):\n",
    "        cnt+=1\n",
    "if(cnt>1):\n",
    "    print(n,\"is not prime\")\n",
    "else:\n",
    "    print(n,\"is prime\") "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "537cade8",
   "metadata": {},
   "source": [
    "# 8. To check whether a number is palindrome or not. .(using recursion and without recursion).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c08678d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# without\n",
    "num = input(\"Enter a number: \")  \n",
    "rev = num[::-1]  \n",
    "\n",
    "if num == rev:  \n",
    "    print(\"Palindrome\")  \n",
    "else:  \n",
    "    print(\"Not Palindrome\")  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79c041f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# using rcursion\n",
    "def is_palindrome(number):\n",
    "    str_number = str(number)\n",
    "\n",
    "    return str_number == str_number[::-1]\n",
    "\n",
    "n = int(input(\"Enter a number: \"))\n",
    "\n",
    "if is_palindrome(n):\n",
    "    print(n,\"is a palindrome.\")\n",
    "else:\n",
    "    print(n,\"is not a palindrome.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26c479a1",
   "metadata": {},
   "source": [
    "# 9. Write a program that asks the user how many Fibonnaci numbers to generate and then generates them.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dbb3a916",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "How many Fibonacci numbers? 5\n",
      "0 1 1 2 3 "
     ]
    }
   ],
   "source": [
    "def fibonacci(n):  \n",
    "    if n <= 1:  \n",
    "        return n  \n",
    "    return fibonacci(n - 1) + fibonacci(n - 2)  \n",
    "\n",
    "num = int(input(\"How many Fibonacci numbers? \"))  \n",
    "for i in range(num):  \n",
    "    print(fibonacci(i), end=\" \")  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "961bc110",
   "metadata": {},
   "outputs": [],
   "source": [
    "fibo=[0,1]   #[0,1,1]\n",
    "for i in range(0,11):  #1\n",
    "    next_term=fibo[i]+fibo[i+1]   \n",
    "    fibo.append(next_term)\n",
    "fibo\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9132409d",
   "metadata": {},
   "source": [
    "# 10. Write a program (using functions!) that asks the user for a long string containing multiple words. Print back to the user the same string, except with the words in backwards order. E.g “ I am Msc student” is :”student Msc am I”\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e1c181be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a sentence: okjcnd\n",
      "okjcnd\n"
     ]
    }
   ],
   "source": [
    "def reverse_words(sentence):  \n",
    "    return \" \".join(sentence.split()[::-1])  \n",
    "\n",
    "text = input(\"Enter a sentence: \")  \n",
    "print(reverse_words(text))  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cc8ecec",
   "metadata": {},
   "source": [
    "# 11. Write a program to implement binary search to search the given element using function.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "034731c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "def binary_search(arr, low, high, x):\n",
    "    if low > high:\n",
    "        return -1  # Element not found\n",
    "    mid = (low + high) // 2\n",
    "    if arr[mid] == x:\n",
    "        return mid  # Element found\n",
    "    elif arr[mid] > x:\n",
    "        return binary_search(arr, low, mid - 1, x)  # Search left\n",
    "    else:\n",
    "        return binary_search(arr, mid + 1, high, x)  # Search right\n",
    "\n",
    "# Test\n",
    "arr = [2, 3, 4, 10, 40]\n",
    "x = 10\n",
    "result = binary_search(arr, 0, len(arr)-1, x)\n",
    "\n",
    "if result != -1:\n",
    "    print(\"Element found at index\", result)\n",
    "else:\n",
    "    print(\"Element not found\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f38237e",
   "metadata": {},
   "source": [
    "# 12. Given a .txt file that has a list of a bunch of names, count how many of each name there are in the file, and print out the results to the screen.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e27d1aa4",
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'names.txt'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[16], line 3\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mcollections\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Counter  \n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnames.txt\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m file:  \n\u001b[0;32m      4\u001b[0m     names \u001b[38;5;241m=\u001b[39m file\u001b[38;5;241m.\u001b[39mread()\u001b[38;5;241m.\u001b[39msplitlines()  \n\u001b[0;32m      6\u001b[0m count \u001b[38;5;241m=\u001b[39m Counter(names)  \n",
      "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\IPython\\core\\interactiveshell.py:286\u001b[0m, in \u001b[0;36m_modified_open\u001b[1;34m(file, *args, **kwargs)\u001b[0m\n\u001b[0;32m    279\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[0;32m    280\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m    281\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    282\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    283\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    284\u001b[0m     )\n\u001b[1;32m--> 286\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m io_open(file, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'names.txt'"
     ]
    }
   ],
   "source": [
    "from collections import Counter  \n",
    "\n",
    "with open(\"names.txt\", \"r\") as file:  \n",
    "    names = file.read().splitlines()  \n",
    "\n",
    "count = Counter(names)  \n",
    "\n",
    "for name, freq in count.items():  \n",
    "    print(f\"{name}: {freq}\")  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5de6f642",
   "metadata": {},
   "source": [
    "# 13. Write a program that takes a list of numbers (e.g., a = [5, 10, 15, 20, 25]) and makes a new list of only the first and last elements of the given list.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba533c7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "numbers = input(\"Enter numbers separated by commas: \").split(',') # Get input from the user\n",
    "\n",
    "numbers = [int(num) for num in numbers] # Convert strings to integers\n",
    "\n",
    "result = [numbers[0], numbers[-1]] #1st and last number \n",
    "\n",
    "print(\"1st and last number is\", result)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea02abb9",
   "metadata": {},
   "source": [
    "# 14. Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized. multiple words. Print back to the user the same string, except with the words in backwards order. E.g “ I am Msc student” is :”student Msc am I”.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c5b5cfbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a sentence:  I am Msc student\n",
      "STUDENT MSC AM I\n"
     ]
    }
   ],
   "source": [
    "text = input(\"Enter a sentence: \").upper()  \n",
    "print(\" \".join(text.split()[::-1]))  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5353d50",
   "metadata": {},
   "source": [
    "# 15. Write a program that accepts a sentence and calculate the number of letters and digits.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d648d7e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def count_letters_digits(sentence):\n",
    "    letters = 0\n",
    "    digits = 0\n",
    "    \n",
    "    for char in sentence:\n",
    "        if char.isalpha():\n",
    "            letters += 1\n",
    "        elif char.isdigit():\n",
    "            digits += 1\n",
    "    \n",
    "    # Print the results\n",
    "    print(f\"Original String: {sentence}\")\n",
    "    print(f\"Number of letters: {letters}\")\n",
    "    print(f\"Number of digits: {digits}\")\n",
    "\n",
    "# Accept the sentence from the user\n",
    "sentence = input(\"Enter a sentence: \")\n",
    "\n",
    "# Call the function \n",
    "count_letters_digits(sentence)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83176c32",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = input(\"Enter a sentence: \")  \n",
    "letters = sum(c.isalpha() for c in text)  \n",
    "digits = sum(c.isdigit() for c in text)  \n",
    "print(\"Letters:\", letters, \"Digits:\", digits)  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29e6d513",
   "metadata": {},
   "source": [
    "# 16. Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a02e7a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def count_upper_lower(sentence):\n",
    "    upper_case = 0\n",
    "    lower_case = 0\n",
    "    \n",
    "  \n",
    "    for char in sentence:\n",
    "        if char.isupper():\n",
    "            upper_case += 1\n",
    "        elif char.islower():\n",
    "            lower_case += 1\n",
    "    \n",
    "    # Print the results\n",
    "    print(f\"Original String: {sentence}\")\n",
    "    print(f\"Upper case: {upper_case}\")\n",
    "    print(f\"Lower case: {lower_case}\")\n",
    "\n",
    "# Accept the sentence from the user\n",
    "sentence = input(\"Enter a sentence: \")\n",
    "\n",
    "# Calling function\n",
    "count_upper_lower(sentence)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5908c1a4",
   "metadata": {},
   "source": [
    "# 17. Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "229a4703",
   "metadata": {},
   "outputs": [],
   "source": [
    "def factorial(n):\n",
    "    # Check number is -ve\n",
    "    if n < 0:\n",
    "        return \"Factorial is not defined for negative numbers.\"\n",
    "    \n",
    "      # for value 0 nad 1 --> 0! = 1 and 1! = 1\n",
    "    elif n == 0 or n == 1:\n",
    "        return 1\n",
    "    \n",
    "    # Recursive case: n! = n * (n-1)!\n",
    "    else:\n",
    "        return n * factorial(n - 1)  \n",
    "\n",
    "# Get input from the user\n",
    "n = int(input(\"Enter a non-negative integer: \"))\n",
    "\n",
    "# Display the factorial \n",
    "print(\"The factorial of\",n, \"is:\" ,factorial(n))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9bba6d4d",
   "metadata": {},
   "source": [
    "# 18. Write a Python program to solve the Fibonacci sequence using recursion.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d7d633e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the number of terms: 5\n",
      "Fibonacci sequence:\n",
      "0,1,1,2,3,"
     ]
    }
   ],
   "source": [
    " # Fibonacci sequence means each number is the sum of the two preceding ones [ formula--> F(n) = F(n-1) + F(n-2) ]\n",
    "\n",
    "def fibonacci(n):\n",
    "    if n <= 0:\n",
    "        return 0\n",
    "    elif n == 1:\n",
    "        return 1\n",
    "    else:\n",
    "        return fibonacci(n-1) + fibonacci(n-2)\n",
    "\n",
    "# Get the number from the user\n",
    "terms = int(input(\"Enter the number of terms: \"))\n",
    "\n",
    "# Display the Fibonacci sequence\n",
    "print(\"Fibonacci sequence:\")\n",
    "for i in range(terms):\n",
    "    print(fibonacci(i), end=\",\")\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "958ce699",
   "metadata": {},
   "source": [
    "# 19. Write a Python program to copy the contents of a file to another file\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2e501952",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File copied successfully.\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "source = r'C:\\Users\\mahes\\Documents\\STUDY FOR LANGUAGE\\web_answers.txt'\n",
    "destination = r'C:\\Users\\mahes\\Documents\\STUDY FOR LANGUAGE\\web_answers_copy.txt'\n",
    "\n",
    "if os.path.exists(source):\n",
    "    with open(source, 'r', encoding='utf-8') as src, open(destination, 'w', encoding='utf-8') as dest:\n",
    "        dest.write(src.read())\n",
    "    print(\"File copied successfully.\")\n",
    "else:\n",
    "    print(\"File not found.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "100b4c52",
   "metadata": {},
   "source": [
    "# 20. Write a Python class named Circle constructed by a radius and two methods which will compute the area and the perimeter of a circle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "700ad823",
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import pi\n",
    "class Circle():\n",
    "    \n",
    "    def __init__(self,r):\n",
    "        self.radius = r\n",
    "\n",
    "    def area(self):\n",
    "        return pi*self.radius**2\n",
    "    \n",
    "    def perimeter(self):\n",
    "        return 2*pi*self.radius\n",
    "\n",
    "NewCircle = Circle(2)\n",
    "print(NewCircle.area())\n",
    "print(NewCircle.perimeter())\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "16ed880d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44ad313e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
