0% found this document useful (0 votes)
7 views

Example program for Loop

The document contains multiple Python code snippets for various mathematical operations including finding numbers divisible by 5 but not by 10, generating Fibonacci series, finding factors of a number, checking if a number is prime or perfect, and printing the first n prime numbers. Each code snippet is accompanied by example inputs and outputs. The document serves as a practical guide for basic programming tasks related to number theory.

Uploaded by

r0761808
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Example program for Loop

The document contains multiple Python code snippets for various mathematical operations including finding numbers divisible by 5 but not by 10, generating Fibonacci series, finding factors of a number, checking if a number is prime or perfect, and printing the first n prime numbers. Each code snippet is accompanied by example inputs and outputs. The document serves as a practical guide for basic programming tasks related to number theory.

Uploaded by

r0761808
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

print nos divisible by 5 not by 10

n=eval(input("enter a"))
for i in range(1,n,1):
if(i%5==0 and i%10!=0):
print(i)

output
enter a:30
5
15
25

Fibonacci series
a=0
b=1
n=eval(input("Enter the number of terms: "))
print("Fibonacci Series: ")
print(a,b)
for i in range(1,n,1):
c=a+b
print(c)
a=b
b=c

output
Enter the number of terms: 6
Fibonacci Series:
01
1
2
3
5
8

find factors of a number


n=eval(input("enter a number:"))
for i in range(1,n+1,1):
if(n%i==0):
print(i)

Output
enter a number:10
1
2
5
10

check the no is prime or not


n=eval(input("enter a number"))
for i in range(2,n):
if(n%i==0):
print("The num is not a prime")
break
else:
print("The num is a prime number.")

output
enter a no:7
The num is a prime number.

check a number is perfect number or not


n=eval(input("enter a number:"))
sum=0
for i in range(1,n,1):
if(n%i==0):
sum=sum+i
if(sum==n):
print("the number is perfect number")
else:
print("the number is not perfect number")

Output
enter a number:6
the number is perfect number

Program to print first n prime numbers


number=int(input("enter no of prime
numbers to be displayed:"))
count=1
n=2
while(count<=number):
for i in range(2,n):
if(n%i==0):
break
else:
print(n)
count=count+1
n=n+1

Output
enter no of prime numbers to be
displayed:5
2
3
5
7
11
Program to print prime numbers in range
lower=eval(input("enter a lower range"))
upper=eval(input("enter a upper range"))
for n in range(lower,upper + 1):
if n > 1:
for i in range(2,n):
if (n % i) == 0:
break
else:
print(n)

output:
enter a lower range50
enter a upper range100
53
59
61
67
71
73
79
83
89
97

You might also like