0% found this document useful (0 votes)
32 views5 pages

Loop Exercises

This document contains Python code snippets for several numerical programs: 1) A prime number checker that takes a number input and checks if it is prime by testing for divisibility by numbers from 2 to the input number. 2) A prime number finder in a range that prints all prime numbers between two given interval values. 3) A factorial finder that calculates the factorial of a given positive number. 4) A Fibonacci sequence generator that prints the Fibonacci sequence up to a given number of terms.

Uploaded by

Nayera Alsayed
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)
32 views5 pages

Loop Exercises

This document contains Python code snippets for several numerical programs: 1) A prime number checker that takes a number input and checks if it is prime by testing for divisibility by numbers from 2 to the input number. 2) A prime number finder in a range that prints all prime numbers between two given interval values. 3) A factorial finder that calculates the factorial of a given positive number. 4) A Fibonacci sequence generator that prints the Fibonacci sequence up to a given number of terms.

Uploaded by

Nayera Alsayed
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/ 5

Looping Exercises

Python Program to Check Prime Number


print("-----------Prime Number---------")
num=int(input("Enter a Number: "))
if num > 1: # prime numbers are greater than 1
for i in range(2, num):
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
Python Program to Print all Prime Numbers in
an Interval
print("-Prime Numbers in a specific interval-")
From,To = eval(input("Enter The interval (ie.., 11,83)"))
print("Prime numbers between",From,"and",To,"are:")
for number in range(From,To+ 1):
if number > 1:
for i in range(2,number):
if (number % i) == 0:
break
else:
print(number)
Python Program to Find the Factorial of a
Number
print("--------Factorial---------")
num = int(input("Enter Number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else: # check if the number greater than 1
for i in range(1,num+1):
factorial = factorial*i
print("The factorial of {0} is {1}".format(num,factorial))
Python Program to Print the Fibonacci
sequence
print("---Fibonacci sequence---")
Numberofterms = int(input("Enter Num of Terms: "))
n1,n2 = 0,1 # first two terms
# check if the number of terms is valid
if Numberofterms <= 0:
print("Please enter a positive integer")
elif Numberofterms == 1:
print("Fibonacci sequence upto",Numberofterms,":",n1)
else:
print("Fibonacci sequence upto",Numberofterms,":")
print(n1,",",n2,end=" , ")
counter = 3
while counter <= Numberofterms:
NextTerm = n1 + n2
print(NextTerm,end=" , ")
# update values
n1 = n2
n2 = NextTerm
counter += 1

You might also like