0% found this document useful (0 votes)
40 views7 pages

N Int (Input ("Enter Number of Terms: ") ) A, B 0, 1 I 1 While I N: Print (A, End ',') A, B B, A+b I+ 1

The document describes a program to calculate the Fibonacci sequence up to a user-specified number of terms (n) using both a while loop and a for loop. The program initializes two variables a and b to the first two Fibonacci numbers (0 and 1), then iteratively calculates each subsequent number as the sum of the previous two and prints the results.

Uploaded by

harish kuna
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)
40 views7 pages

N Int (Input ("Enter Number of Terms: ") ) A, B 0, 1 I 1 While I N: Print (A, End ',') A, B B, A+b I+ 1

The document describes a program to calculate the Fibonacci sequence up to a user-specified number of terms (n) using both a while loop and a for loop. The program initializes two variables a and b to the first two Fibonacci numbers (0 and 1), then iteratively calculates each subsequent number as the sum of the previous two and prints the results.

Uploaded by

harish kuna
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/ 7

The Fibonacci sequence starts with 0,1 and each number in the sequence

(after the first two) is the sum of the previous two, i.e., the sequence is 0, 1,
1, 2, 3, 5, 8, . . ... Write a program that computes and outputs the Fibonacci
number upto n terms, where n is a value entered by the user.

n=int(input("Enter number of terms : "))


a, b = 0, 1
i=1
while i <= n:
print(a, end=',')
a, b = b, a+b # Recall swap in Python
i+=1

Write the above program using for loop


17
19-Aug-19 Debajyoti Ghosh, Asst. Prof, BML Munjal University
Write the previous program using for loop

n=int(input("Enter number of terms : "))


a, b = 0, 1
for i in range(1,n+1): # range(0,n) also works
print(a,end=',')
a, b = b, a+b

20-Aug-19 Debajyoti Ghosh, Asst. Prof, BML Munjal University 18


Fibonacci sequence in the range 0-100

The following code prints Fibonacci sequence in the range 0-100

def fib(n):
a, b = 0, 1
while a < n: # Run the program with b < n and see what it prints
print(a,end=',')
a, b = b, a+b
fib(100)

19-Aug-19 Debajyoti Ghosh, Asst. Prof, BML Munjal University 19


Implements the Euclidian algorithm to find G.C.D/H.C.F(the largest integer that
can be evenly divided into both of them) of two positive integers
x,y=eval(input("Enter two numbers : "))
while y:
x, y = y, x % y # Recall swap in Python
print(x)

How to find L.C.M(Least Common Multiple, lcm of two numbers x and y is the smallest positive integer
divisible by both x and y) of two numbers using G.C.D(or H.CF.) of two numbers?
#lcm(x,y) = (x*y)/gcd(x,y)
x,y=eval(input("Enter two numbers : "))
x1,y1=x,y
while y:
x, y = y, x % y # Recall swap in Python
print("lcm = ",(x1*y1)//x)
How to find lcm(the smallest integer that divides both) without using gcd?
20
19-Aug-19 Debajyoti Ghosh, Asst. Prof, BML Munjal University
GCD of more than 2 integers

Write a function to find GCD of three numbers (using GCD of two numbers)

Write a function to find GCD of more than two (or array) numbers

19-Aug-19 Debajyoti Ghosh, Asst. Prof, BML Munjal University 21


Primality testing
Write a Python function that takes a number (n>1) as a parameter and check whether the number is prime or not.
import math
def prime_test(n): #n>1
for i in range(2,int(math.sqrt(n))+1, 1):
if n % i==0:
print('Not Prime')
break
else:
print('Prime')
prime_test(4)
prime_test(5)
prime_test(6)
prime_test(8)
prime_test(10)
prime_test(11)
prime_test(12)
prime_test(13)

Write the above program without using math module and sqrt or pow function
22
20-Aug-19 Debajyoti Ghosh, Asst. Prof, BML Munjal University
sin(x) infinite sum series
Calculate the value of sin(x) using the following Taylor series expansion of sin(x)

# This program generate and evaluate sin(x) function upto 0.000001% accuracy
import math
x=float(input(" Enter the value for x : ")) # where x is the angle in radians
x=(x*math.pi)/180 # convert degree into radian
# or x=math.radians(x) #Convert angle x from degrees to radians
term, sum, i=x, x, 2
while abs(term) >= 0.000001:
# until the absolute value of the nth term is less than the desired acceptable error
# Determine the number of terms in the above expansion so as to ensure that the difference between the
# computed value of sin(x) and standard value of sin(x) should not be more than 0.000001.
term *= (-x*x)/((2*i-2)*(2*i-1))
sum=sum+term
i+=1
print("The value of Sin(x) = ", sum)
print("The value of Sin(x) = ", math.sin(x))
Write a program for cos(x) infinite sum series 23

You might also like