PYTHON | VARIOUS USER DEFINED FUNCTIONS


#User Defined Functions in Python

def Add(x,y):
    z=x+y
    return z


#main
num1=int(input("Enter 1st Number:"))
num2=int(input("Enter 2nd Number:"))
num3=Add(num1,num2)
print("The Addition of",num1,"and",num2,"is",num3)

'''
Write a python method/function Count3and7(N) to find and display the count of all those numbers which are between 1 and N, which are either divisible by 3 or by 7. 2
For example: If the value of N is 15
The sum should be displayed as 7
(as 3,6,7,9,12,14,15 in between 1 to 15 are either divisible by 3 or 7)
'''

def Count3and7(N):
    count=0
    for i in range(1,N+1):
        if i%3==0 or i%7==0:
            print(i,end=", ")
            count=count+1
    print("\nTotal Count =",count)


x=int(input("Enter Number: "))
Count3and7(x)
def Cube(x):1
    r=x**3
    return r


#Function to find factorial f N
def Factorial(n):
    p=1
    for i in range(1,n+1):
        p=p*i
    return p

num=int(input("Enter Number:"))
f=Factorial(num)
print("\nFactorial of",num,"is",f)
def Factors(num):
    n=1
    print("Factors of",num,"are:")
    while n<=num:
        if(num%n==0):
            print(n,end=" ")
        n=n+1

n=int(input("Enter Number:"))
Factors(n)
print()


#Function to find the HCF of 3 Nos.
def HCF(x=10, y=20, z=30):
    prod=x*y*z
    hcf=1
    for i in range(2,prod+1):
        if x%i==0 and y%i==0 and z%i==0:
            hcf=i
            break
    return hcf

#main
n=HCF(75)
print("HCF is:",n)
a=eval(input("Enter 1st Number:"))
b=eval(input("Enter 2nd Number:"))
c=eval(input("Enter 3rd Number:"))

n=HCF(a,b,c)
print("HCF is:",n)

n=HCF(a,b)
print("HCF is:",n)


#Function to check the number is Palindrome or Not.
def isPalindrome(num):
    rev=0
    x=num
    while num!=0:
        rem = num%10
        rev = rev*10 + rem
        num=num//10
    if(x==rev):
        return True
    else:
        return False

n=int(input("Enter Number:"))
if isPalindrome(n):
    print(n,'is a palindrome number')
else:
    print(n,'is not a palindrome number')
def MaxOf3Nos(x,y,z):
    if x>y and x>z:
        return x
    elif y>x and y>z:
        return y
    elif z>x and z>y:
        return z
    return z

#main
a=eval(input("Enter 1st Number:"))
b=eval(input("Enter 2nd Number:"))
c=eval(input("Enter 3rd Number:"))

m=MaxOf3Nos(a,b,c)
print("Maximum number is:",m)


#Function to find the Simple Interest.
def SimpleInterest(p,r,n):
    i=p*r*n*0.01
    return i


No comments

Post your comments

Powered by Blogger.