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

Python Functions for Primes, Factorials, Odds & Evens

Uploaded by

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

Python Functions for Primes, Factorials, Odds & Evens

Uploaded by

dhanushkumara212
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Ex no 5 Print Prime Numbers Less than 20 2510545

print("\t\t\t Print prime numbers less than 20")

def print_primes(n):

def is_prime(num):

if num<2:

return False

for i in range(2,int(num**0.5)+1):

if num% i==0:

return False

return True

i=0

while i<n:

if is_prime(i):

print(i)

i+=1

print_primes(20)
Output:

Print prime numbers less than 20

11

13

17

19
Ex no 6 Factorial of given numbers using 2510545
Recursive Function

def fact(n):

if n==0:

return 1

else:

return n*fact(n-1)

print("\t\t\t factorial of a number")

num=int(input("enter a non negative integer:"))

factorial = fact(num)

print("the factorial of",num,"is",factorial)


Output:

Factorial of a number

Enter a non negative integer:10

The Factorial of 10 is 3628800


Ex No 7 Count Number of Even and Odd Number 2510545
from Array of N Numbers

print("\t\t\t Count the number of even and odd numbers")

arr_input=input("Enter elements of the array:")

arr=[int(x)for x in arr_input.split()]

n=len(arr)

counteven=0

countodd=0

for i in range(n):

if arr[i]%2==0:

counteven+=1

else:

countodd+=1

print("Even elements count:",counteven)

print("Odd elements count:",countodd)


Output:

Count the number of even and odd numbers

Enter elements of the array:1 2 3 4 5 6 7 8 9 10

Even elements count: 5

Odd elements count: 5

You might also like