THEORY

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

PROGRAM 1

#CODE TO MAKE A PROGRAM WHICH TAKES 2 VALUES AND


PERFORMS ALL ARITHMETIC OPERATIONS

num1 = int(input("Enter first number: "))


num2 = int(input("Enter second number: ")) print("Addition: ",
num1 + num2)
print("Subtraction: ", num1-num2)
print("Multiplication: ", num1 * num2)
print("Division: ", num1 / num2)
print("Floor Division: ", num1 // num2)
print("Modulus: ", num1 % num2)

OUTPUT:-
PROGRAM 2
#CODE TO CHECK IF THE IMPUTED NUMBER IS PERFECT OR
NOT

n = int(input("Enter any number: "))


sum1 =0
for i in range(1, n):
if(n % i == 0):
sum1=sum1 + i
if (sum1 == n):
print("The number", n, "is a Perfect number!")
else:
print("The number", n, "is not a Perfect number!")

OUTPUT:-
PROGRAM 3
#CODE TO CHECK IF THE ENTERED NUMBER IS ARMSTRONG
OR NOT
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")

OUTPUT:-
PROGRAM 4
#CODE TO FIND FACTORIAL OF THE ENTERED NUMBER

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


factorial = 1
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

OUTPUT:-
PROGRAM 5
#CODE TO PRINT THE FIBONACCI SERIES TILL THE IMPUTED VALUE
n1, n2 = 0, 1
count = 0
nterms = int(input("For how many terms you want to print the
seeries:"))
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1,end=" ")
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
OUTPUT:-
PROGRAM 6
#CODE TO CHECK IF THE ENTERED STRING IS PALINDROME OR
NOT

val = input("Enter a word: ")


word = val.lower()
wlen= len(word)
x=0
for i in range(len(word)):
if word[i] == word [-1-i]:
x+=1
if x==wlen:
print(val, "is a palindrome.")
else :
print(val, "is not a palindrome.")

OUTPUT:-
PROGRAM 7
#CODE TO RECURSIVELY FIND THE FACTORIAL OF A NATURAL
NUMBER

def recur(n):
if n == 1:
return n
else:
return n*recur(n-1)
num =int(input("Enter a number to find factorial:"))
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur(num))

OUTPUT:-
PROGRAM 8
#CODE TO READ A CSV FILE

import csv
with open('poem.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)

OUTPUT:-
PROGRAM 9
#CODE TO REMOVE ALL LINE THAT CONTAINS ‘A’ IN A FILE AND
WRITE THEM IN ANOTHER FILE
PROGRAM 10
#CODE TO READ A TEXT FILE AND DISPLAY THE NUMBERS OF
VOWELS/CONSONANTS/UPPERCASE/LOWERCASE CHARACTERS
v=c=u=l=0
x=open('poem.txt','r')
r=x.read()
for i in r:
if i in "aeiouAEIOU": #vovel count
v+=1
if i.lower() in "bcdfghjklmnpqrstvwxyz": #consonants count
c+=1
if i.isupper(): #upper case count
u+=1
if i.islower(): #lower case count
l+=1
print("Number of vowels in flie:",v)
print("Number of consonants in flie:",c)
print("Number of uppercase in flie:",u)
print("Number of lowercase in flie:",l)
x.close()
OUTPUT:-

PROGRAM 11
#CODE

You might also like