#PYTHON(DAY38)
Example Programs
1) Python Program to Find ASCII Value of Character
# Program to find the ASCII value of the given character
c = 'p'
print("The ASCII value of '" + c + "' is", ord(c))
And the output is:
The ASCII value of 'p' is 112
2) Python Program to Find HCF or GCD
# Python program to find H.C.F of two numbers
# define a function
def compute_hcf(x, y):
# choose the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
num1 = 54
num2 = 24
print("The H.C.F. is", compute_hcf(num1, num2))
And the output is:
The H.C.F. is 6
3) Python Program to Find LCM
# Python Program to find the L.C.M. of two input number
def compute_lcm(x, y):
# choose the greater number
if x > y:
greater = x
else:
greater = y
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
num1 = 54
num2 = 24
print("The L.C.M. is", compute_lcm(num1, num2))
And the output is:
The L.C.M. is 216
4) Python Program to Find the Factors of a Number
# Python Program to find the factors of a number
# This function computes the factor of the argument passed
def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)
num = 320
print_factors(num)
And the output is:
The factors of 320 are:
1
2
4
5
8
10
16
20
32
40
64
80
160
320
No comments:
Post a Comment