Python Functions example

# Python Function demo

print '*** Select the operation you would like to perform *****'
print '\n 1. Prime Number between given interval \n 2. Armstrong Number \n 3. Even or Odd \n 4. Factorial Of number \n'
option=input('Option:')
message='********* Welcome to python programming *****'
def test(message):
print message
return;
test(message);

def prime_number(prime_range):
for num in range(2,prime_range+1):
for i in range(2, num):
if (num%i==0):
break
else:
print num
return;

def armstrongNumber(num):
temp=num;
sum=0;
while temp >1:
rem=temp%10;
sum+=rem**3
temp
if sum==num:
print (num,'is an Armstrong Number')
else:
print (num,'is not an Armstrong Number')
return;

def evenodd(num):
if(num%2==0):
print (num,'is Even Number')
else:
print (num,'is Odd Number')
return;

def factorial(num):
fact=1
if(num < 1):
print '\n Factorial doesnt exist for negative numbers'
elif num==0:
print '\n Factorial of 0 is 1'
else:
for i in range(1, num+1):
fact=fact*i;
print ('\n Factorial of ',num,' is: ',fact)
return;

if option==1:
prime_range=input('\n Enter the range till you want to find prime numbers:')
prime_number(prime_range);

elif option==2:
num=input('\n Enter the number to check if it is armstrong or not: ')
armstrongNumber(num);

elif option==3:
num=input('\n Enter the number to find even or odd: ')
evenodd(num);

elif option==4:
num=input('\n Enter the number to find factorial: ')
factorial(num);
else:
print '\n Invalid option'

0 Comments:

Post a Comment



Newer Post Older Post Home