0% found this document useful (0 votes)
58 views5 pages

Practicals 11

The document contains Python programs to check if a number is Armstrong, perfect, or prime; check if a string is a palindrome; find the Fibonacci series, LCM, and HCF of numbers; count vowels, digits, and characters in a string; find the longest consonant substring; capitalize alternating letters in a string; perform arithmetic operations based on user selection; determine a grade based on marks; and calculate the factorial of a number. The programs take user input, perform various calculations and logical checks on the input, and output the results.

Uploaded by

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

Practicals 11

The document contains Python programs to check if a number is Armstrong, perfect, or prime; check if a string is a palindrome; find the Fibonacci series, LCM, and HCF of numbers; count vowels, digits, and characters in a string; find the longest consonant substring; capitalize alternating letters in a string; perform arithmetic operations based on user selection; determine a grade based on marks; and calculate the factorial of a number. The programs take user input, perform various calculations and logical checks on the input, and output the results.

Uploaded by

Adithya Vinod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

# Python program to check if the number is an Armstrong number or not

# abcd... = a**3 + b**3 + c**3 + d**3 + ...

num = int(input("Enter a number: "))

sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

# Python program to check whether the given number is a perfect number or not

num = int(input("Enter a number : "))


sum = 0
for i in range (1,num):
if(num % i == 0):
sum = sum + i
if(sum == num):
print(num,"is a perfect number")
else:
print(num,"is not a perfect number")

for i in '10101':
print (i)

# Python program to check if a string is palindrome or not


str1 = input("Enter a string")
str2 = " "
for i in str1:
str2 = i+str2
print('Str2',str2)

if (str2 == str1):
print("String is a palindrome")
else:
print("String is not a palindrome")

#Input a number and check if the number is a prime or composite number


num = int(input("Enter any number : "))
if num > 1:
for i in range(2, num):
#print('num',num, end=' ')
#print('i',i)
if (num % i) == 0:
print(num, "is NOT a prime number")
break
else:
print(num, "is a PRIME number")
elif num == 0 or 1:
print(num, "is a neither prime NOR composite number")
else:
print(num, "is NOT a prime number it is a COMPOSITE number")

#Display the terms of a Fibonacci series

def fibonacci(n):
a=0
b=1
if n <= 0:
print("Incorrect input")
elif n == 1:
print (b)
else:
print (a)
print (b)
for i in range(2,n+1):
c=a+b
a=b
b=c
print (b, end= ' ')

num=int(input("Enter a number to which Fibonacci is to be generated"))


fibonacci(num)

# 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 =int(input("Enter first number "))


#num2 = int(input("Enter second number "))

print("The L.C.M. is", compute_lcm(num1, num2))


# Python Program to find the H.C.F. of two input number

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 =int(input("Enter first number "))


num2 = int(input("Enter second number "))

print("The H.C.F. is", compute_hcf(num1, num2))

#Count and display the number of vowels, characters, digits, spaces in string
str = input('Enter the string : ')
vowels = 0
digits = 0
spaces = 0
ch=0
str = str.lower()

for i in range(0, len(str)):


if(str[i] == 'a' or str[i] == 'e' or str[i] == 'i' or str[i] == 'o' or str[i] == 'u' or str[i] == 'A' or str[i] == 'E' or str[i]
== 'I' or str[i] == 'O' or str[i] == 'U'):
vowels = vowels + 1

elif((str[i] >= 'a' and str[i] <= 'z')):


ch+= 1

elif ((str[i] == ' ')):


spaces+=1

elif ((str[i] >= '0' and str[i] <= '9')):


digits=digits + 1

print("Vowels :",vowels)
print('Digits: ', digits);
print('White spaces: ', spaces);
print ("Characters :",ch)

#program that displays the longest substring of given string having just the consonsnts
string=input('Enter a string ' )
length=len(string)
maxlength= 0
maxsub ='' #empty string
sub=''# empty string
lensub=0
for a in range(length):
if string[a] in 'aeiou' or string[a] in 'AEIOU':
if lensub > maxlength:
maxsub = sub
maxlength= lensub
sub =''
lensub = 0
else:
sub += string[a]
lensub =len(sub)
a+=1
print ("Maximum length consonant substring is" , maxsub, end =' ')
print ("with", maxlength, "characters")

#Write a program that reads a string and then prints a string that capitalizes every other letter in the
string e.g,

string= input( "Enter a string:")


length= len (string)
print(length)
print ("Original string : ", string)
string2= "" # empty string
for a in range (0, length, 2):
string2 += string[a]
if a < (length-1):
string2 += string[a+1].upper()
print ("Alternatively capitalized string", string2)

#Program to perform Arithmetic operations

print("1. Sum of two numbers")


print("2. Subtraction of two numbers")
print("3. Multiplication of two numbers")
print("4. Division of two numbers")
choice=int(input('Enter your choice'))
if choice==1 :
a=int(input('Enter first number'))
b=int(input('Enter second number'))
c=a+b
print("Sum=",c)
elif choice==2 :
a=int(input('Enter first number'))
b=int(input('Enter second number'))
c=a-b
print("Subtraction=",c)
elif choice==3 :
a=int(input('Enter first number'))
b=int(input('Enter second number'))
c=a*b
print("Multiplication=",c)
elif choice==4 :
a=int(input('Enter first number'))
b=int(input('Enter second number'))
c=a/b
print("Division=",c)
else :
print("Wrong choice")

#Program to display grade by taking input of marks.

marks=int(input("Enter marks of the student :"))


if marks > 90:
print("You grade is 'A1' ")
elif marks > 80 and marks <=90:
print("You grade is 'A2' ")
elif marks > 70 and marks <=80:
print("You grade is 'B1' ")
elif marks > 60 and marks <=70:
print("You grade is 'B2' ")
elif marks > 50 and marks <=60:
print("You grade is 'C1' ")
elif marks > 40 and marks <=50:
print("You grade is 'C2' ")
elif marks > 30 and marks <=40:
print("You grade is 'D' ")
elif marks > 20 and marks <=30:
print("You grade is 'E1' ")
elif marks > 0 and marks <=20:
print("You grade is 'E2' ")

#Program to find factorial of the entered number.


num = int(input("Enter the number for calculating its factorial : "))
fact = 1
i=1
while i<=num:
fact = fact*i
i=i+1
print("The factorial of ",num,"=",fact)

You might also like