Skip to content

amitpythondev/module4

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

module4

Functions and modules in python


Task 1: Calculate Factorial Using a Function

Problem Statement: Write a Python program that:

  1. Defines a function named factorial that takes a number as an argument and calculates its factorial using a loop or recursion.
  2. Returns the calculated factorial.
  3. Calls the function with a sample number and prints the output.

🧾 Code Description:

This Python program calculates the factorial of a user-provided number using a recursive function.


🔍 Line-by-Line Explanation:

# Define a function named factorial that takes one argument n
def factorial(n):
    if n < 2:
        return 1
    else:
        return n * factorial(n - 1)
  • This defines a recursive function called factorial.

  • The function works as follows:

    • If n is less than 2 (i.e., 0 or 1), it returns 1. This is the base case because the factorial of 0 or 1 is always 1.
    • Otherwise, it multiplies n by the factorial of n - 1, calling itself. This is the recursive case, which continues until the base case is reached.

n = input("Please enter a number to get factorial : ")
n = int(n)
  • These two lines:

    • Ask the user to enter a number.
    • Convert the input from a string to an integer using int().

result = factorial(n)
  • This line calls the factorial function with the user’s input value and stores the result in the variable result.

print(f"Factorial of {n} is : ", result)
  • This prints the final output to the screen, showing the factorial of the entered number.

🧠 What is a Factorial?

The factorial of a number n (written as n!) is the product of all positive integers from 1 to n.

For example:

  • 5! = 5 × 4 × 3 × 2 × 1 = 120
  • 3! = 3 × 2 × 1 = 6
  • 0! = 1 (by definition)

🧪 Sample Output:

Please enter a number to get factorial : 6
Factorial of 6 is :  720

Task 2: Using the Math Module for Calculations

Problem Statement: Write a Python program that:

  1. Asks the user for a number as input.
  2. Uses the math module to calculate the: o Square root of the number o Natural logarithm (log base e) of the number o Sine of the number (in radians)
  3. Displays the calculated results.

🧾 Purpose of the Code:

This Python program:

  1. Takes a number as input from the user.

  2. Calculates:

    • The square root
    • The natural logarithm (log base e)
    • The sine (in radians)
  3. Displays the results.


🔍 Line-by-Line Explanation:

import math
  • Imports the built-in math module, which provides access to mathematical functions like sqrt(), log(), and sin().

num = int(input("Please enter a number : "))
  • Prompts the user to enter a number.
  • The input is read as a string and then converted to an integer using int().

if num > 0:
    square_root = math.sqrt(num)
    natural_log = math.log(num)
else:
    square_root = "Undefined for non-positive numbers"
    natural_log = "Undefined for non-positive numbers"
  • Conditional check to ensure num is positive:

    • If num > 0:

      • math.sqrt(num) calculates the square root.
      • math.log(num) calculates the natural logarithm (ln), which is only valid for positive numbers.
    • If num <= 0:

      • Both square root and log are set to text strings explaining they are undefined (since these operations aren't valid for zero or negative numbers in the real number system).

sine_value = math.sin(num)
  • Calculates the sine of the number, assuming the input is in radians.
  • The math.sin() function works for any real number (positive, negative, or zero).

print("\nCalculated Results:")
print(f"Square Root of {num}       : {square_root}")
print(f"Natural Logarithm of {num} : {natural_log}")
print(f"Sine of {num} radians      : {sine_value}")
  • Displays the results in a nicely formatted output using f-strings.

  • The results include:

    • Square root
    • Natural logarithm (ln)
    • Sine value (in radians)

Example Output:

If the user enters 25, the output will be:

Please enter a number : 25

Calculated Results:
Square Root of 25       : 5.0
Natural Logarithm of 25 : 3.2188758248682006
Sine of 25 radians      : -0.13235175009777303

About

Functions and modules in python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages