[go: up one dir, main page]

0% found this document useful (0 votes)
5 views6 pages

Python Assignment: #Source Code

The document contains a Python assignment with multiple functions to perform mathematical operations such as calculating HCF, LCM, permutations (nPr), combinations (nCr), and checking Fibonacci and prime numbers. It includes a menu-driven interface for user interaction, allowing users to select the desired operation. The code also handles invalid inputs and provides output based on user choices.

Uploaded by

vratteshanand308
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)
5 views6 pages

Python Assignment: #Source Code

The document contains a Python assignment with multiple functions to perform mathematical operations such as calculating HCF, LCM, permutations (nPr), combinations (nCr), and checking Fibonacci and prime numbers. It includes a menu-driven interface for user interaction, allowing users to select the desired operation. The code also handles invalid inputs and provides output based on user choices.

Uploaded by

vratteshanand308
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/ 6

6

PYTHON ASSIGNMENT

#VRATTESH ANAND
#CLASS-12 SECTION-B
#ROLL NO: 35
#Q2:
#SOURCE CODE
def function_1():
a=int(input("enter a number"))
b=int(input("enter a number"))
while True:
r=a%b
p=a*b
if r==0:
break
a=b
b=r
print('lcm',p//b)
print('hcf',b)

def function_2():

n = int(input("Enter value of n (positive integer): "))


r = int(input("Enter value of r (positive integer): "))
def factorial(x):
f = 1
for k in range(1, x + 1):
f = f * k
return f

if r > n:
print("Invalid input: r should not be greater than n.")
else:
fact_n = factorial(n)
fact_r = factorial(r)
7

fact_n_r = factorial(n - r)

nPr = fact_n // fact_n_r


nCr = fact_n // (fact_r * fact_n_r)

print("nPr ({}P{}) =", nPr)


print("nCr ({}C{}) =", nCr)

def function_3():
n=int(input("enter a number"))
f1=0
f2=1
while True:
if f1+f2>n:
break
f1=f2
f2=f1+f2
if f1==0 or f2==n:
print('fibonacci number')
else:
print('not fibonacci')
n=int(input("enter a number"))
prime=True
for k in range(2,n//2):
if n%k==0:
prime=False
break
if prime==True:
print('prime')
else:
print('composite')

def function_4():
num=int(input("enter a number"))
if num < 2:
return False
i = 2
while i < num:
8

if num % i == 0:
return False
i = i + 1
return True

def fibonacci_primes():
n=int(input("enter a numbeer"))
a = 0
b = 1
cn = 0

print("Fibonacci primes are:")

while cn < n:
if n==prime:
print(a)
c = a + b
a = b
b = c
cn = cn + 1
print(c,b,a)

while True:
print("\n================== MENU ==================")
print("1. CALCULATE HCF AND LCM OF TWO NUMBERS")

print("2. CALCULATE nPr AND nCr")


print("3. CALCULATE AND DISPLAY NUMBERS OF FIBONACCI ")
print("4. CALCULATE AND DISPLAY PRIME NUMBERS OF FIBONACCI
SERIES")
print("0. EXIT")
9

print("==========================================")

ch = int(input("Enter your choice: "))

if ch == 1:
function_1()
elif ch == 2:
function_2()
elif ch == 3:
function_3()
elif ch == 4:
function_4()

elif ch == 0:
print("Exiting the program. Thank you!")
break
else:
print("Invalid choice. Please try again.")

#OUTPUT:
'''
Python 3.13.2 (tags/v3.13.2:4f8bb39, Feb 4 2025, 15:23:48) [MSC
v.1942 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more
information.

========= RESTART:
C:\Users\neels\Documents\PYTHON-FILES\question 2.py =========

================== MENU ==================


1. CALCULATE HCF AND LCM OF TWO NUMBERS
2. CALCULATE nPr AND nCr
3. CALCULATE AND DISPLAY NUMBERS OF FIBONACCI
1. CALCULATE AND DISPLAY PRIME NUMBERS OF FIBONACCI SERIES
0. EXIT
==========================================
Enter your choice: 2
Enter value of n (positive integer): 3
Enter value of r (positive integer): 4
10

Invalid input: r should not be greater than n.

================== MENU ==================


1. CALCULATE HCF AND LCM OF TWO NUMBERS
2. CALCULATE nPr AND nCr
3. CALCULATE AND DISPLAY NUMBERS OF FIBONACCI
1. CALCULATE AND DISPLAY PRIME NUMBERS OF FIBONACCI SERIES
0. EXIT
==========================================
Enter your choice: 2
Enter value of n (positive integer): 1
Enter value of r (positive integer): 1
nPr ({}P{}) = 1
nCr ({}C{}) = 1

================== MENU ==================


1. CALCULATE HCF AND LCM OF TWO NUMBERS
2. CALCULATE nPr AND nCr
3. CALCULATE AND DISPLAY NUMBERS OF FIBONACCI
1. CALCULATE AND DISPLAY PRIME NUMBERS OF FIBONACCI SERIES
0. EXIT
==========================================
Enter your choice: 3
enter a number11
not fibonacci
enter a number12
composite

================== MENU ==================


1. CALCULATE HCF AND LCM OF TWO NUMBERS
2. CALCULATE nPr AND nCr
3. CALCULATE AND DISPLAY NUMBERS OF FIBONACCI
1. CALCULATE AND DISPLAY PRIME NUMBERS OF FIBONACCI SERIES
0. EXIT
==========================================
Enter your choice: 4
enter a number222

================== MENU ==================


11

1. CALCULATE HCF AND LCM OF TWO NUMBERS


2. CALCULATE nPr AND nCr
3. CALCULATE AND DISPLAY NUMBERS OF FIBONACCI
1. CALCULATE AND DISPLAY PRIME NUMBERS OF FIBONACCI SERIES
0. EXIT
==========================================
Enter your choice: 4
enter a number02

================== MENU ==================


1. CALCULATE HCF AND LCM OF TWO NUMBERS
2. CALCULATE nPr AND nCr
3. CALCULATE AND DISPLAY NUMBERS OF FIBONACCI
1. CALCULATE AND DISPLAY PRIME NUMBERS OF FIBONACCI SERIES
0. EXIT
==========================================
'''

You might also like