0% found this document useful (0 votes)
56 views3 pages

Practical Set:-3: 14. Write A Program To Find The Factorial of Given Number Using Recursive Function

This document contains summaries of Python programs that perform various tasks like calculating factorials using recursion and iteration, checking if three numbers are equal and returning their sum tripled, generating a Fibonacci series, performing basic math operations using functions, finding the length of each line in a file, and printing the first 15 bytes of a file. The programs demonstrate the use of functions, file handling, recursion, loops, and conditional statements in Python.

Uploaded by

52 Nevil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views3 pages

Practical Set:-3: 14. Write A Program To Find The Factorial of Given Number Using Recursive Function

This document contains summaries of Python programs that perform various tasks like calculating factorials using recursion and iteration, checking if three numbers are equal and returning their sum tripled, generating a Fibonacci series, performing basic math operations using functions, finding the length of each line in a file, and printing the first 15 bytes of a file. The programs demonstrate the use of functions, file handling, recursion, loops, and conditional statements in Python.

Uploaded by

52 Nevil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

7/13/2021 Practical Set 3 - Jupyter Notebook

Practical set :- 3
Practicals implemented by :- 190760107027

14. Write a program to find the factorial of given number


using recursive function.
In [16]: 1 num = int(input("Enter the number :- "))
2 ​
3 def fact(num):
4 if num >= 1:
5 return num * fact(num - 1)
6 else:
7 return 1
8 print(f"Factorial value of {num} is {fact(num)}")

Enter the number :- 6

Factorial value of 6 is 720

In [17]: 1 # Factorial using iteration method


2 num = int(input("Enter the numner :- "))
3 fact = 1
4 for i in range(1, num+1):
5 fact = fact * i
6 print(f"Factorial value of {num} is {fact}")

Enter the numner :- 6

Factorial value of 6 is 720

15. Write a python function to calculate the sum of three


given numbers, if the values are equal then returns trice of
their sum
In [20]: 1 def sum(num1, num2, num3):
2 sum = 0
3 if num1 == num2 == num3:
4 sum = num1 + num2 + num3
5 return 3 * sum
6 else:
7 return num1 + num2 + num3
8
9 num1, num2, num3 = [eval(num) for num in input("Enter the number :- ").split
10 print(f"Return value of sum function is: {sum(num1, num2, num3)}")

Enter the number :- 10 10 10

Return value of sum function is: 90

localhost:8888/notebooks/PDS Practical/Practical Set 3.ipynb 1/3


7/13/2021 Practical Set 3 - Jupyter Notebook

16. Write a program to generate Fibonacci series for N


terms using function
In [12]: 1 def fibonacci(num):
2 if num == 0 or num == 1:
3 print("0, 1, ")
4 else:
5 print("0, 1, ", end=" ")
6 f1 = 0
7 f2 = 1
8 for i in range(1, num-1):
9 f3 = f1 + f2
10 f1 = f2
11 f2 = f3
12 print(f"{f2}, ",end=" ")
13
14 ​
15 num = int(input("Enter the value of terms :- "))
16 fibonacci(num)

Enter the value of terms :- 7

0, 1, 1, 2, 3, 5, 8,

17. Write a python program to perform the following


operation for addition, subtraction, multiplication, division
and modulo using function.
In [27]: 1 def calculator(num1, num2):
2 sum = num1 + num2
3 sub = num1 - num2
4 multi = num1 * num2
5 div = num1 / num2
6 modulo = num % num2
7 return sum, sub, multi, div, modulo
8 num1, num2 = float(input("Enter number :- ")), float(input("Enter the number
9 sum, sub, multi, div, modulo = calculator(num1, num2)
10 print(f"Sum :- {sum}\nSubtraction :- {sub}\nMultiplication :- {multi}\nDivis

Enter number :- 6

Enter the number :- 5

Sum :- 11.0

Subtraction :- 1.0

Multiplication :- 30.0

Division :- 1.2

Modulo :- 1.0

18. Write a python program to print the length of each line


in the file.

localhost:8888/notebooks/PDS Practical/Practical Set 3.ipynb 2/3


7/13/2021 Practical Set 3 - Jupyter Notebook

In [4]: 1 with open('demoFile.txt', 'r') as file:


2 for line in file:
3 print(f"{line} : {len(line)}")

Welcome

: 8

to

: 3

python programming

: 19

have a pleasent journey of

: 27

coding : 6

19. Write a python program to print first 15 bytes from file


using with open
In [6]: 1 with open("demoFile.txt") as file:
2 print(file.read(15))

Welcome

to

pyth

localhost:8888/notebooks/PDS Practical/Practical Set 3.ipynb 3/3

You might also like