Program 1
Program 1
PROGRAM 01
1. Write a program to show whether entered
numbers are prime or not in the given range .
lower=int(input("Enter lowest number as lower bound to check : "))
upper=int(input("Enter highest number as upper bound to check: "))
c=0
for i in range(lower, upper+1):
if (i == 1):
continue
OUTPUT 01
PROGRAM 02
2. Write a program to input a string and
determine whether it is a palindrome or not.
string=input('Enter a string:')
length=len(string)
mid=length//
rev=-1
for a in range(mid):
if string[a]==string[rev]:
print(string,'is a palindrome.')
break
else :
print(string,'is not a palindrome.')
OUTPUT 02
PROGRAM 03
3. Write a program to read a text file and
display the number of vowels/ consonants/
uppercase/ lowercase characters and other
than character and digit in the file.
filein = open("Mydoc1.txt",'r')
line = filein.read()
count_vow = 0
count_con = 0
count_low = 0
count_up = 0
count_digit = 0
count_other = 0
print(line)
for ch in line:
if ch.isupper():
count_up +=1
if ch.islower():
count_low += 1
if ch in 'aeiouAEIOU':
count_vow += 1
if ch.isalpha():
count_con += 1
if ch.isdigit():
count_digit += 1
print("Digits",count_digit)
print("Vowels: ",count_vow)
print("Consonants: ",count_con-count_vow)
filein.close()
FILE
OUTPUT 03
PROGRAM 04
4. Write a recursive code to compute the nth
Fibonacci number.
def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return(fibonacci(n-2) + fibonacci(n-1))
OUTPUT 04
PROGRAM 05
5. Write a program to read a text file line by line
and display each word separated by a #.
FILE
OUTPUT 05
PROGRAM 06
6. Write a program to perform read and write
operation onto a student.csv file having fields
as roll number, name, stream and percentage.
import csv
with open('Student_Details.csv','w',newline='') as csvf:
writecsv=csv.writer(csvf,delimiter=',')
choice='y'
while choice.lower()=='y':
rl=int(input("Enter Roll No.: "))
n=input("Enter Name: ")
p=float(input("Enter Percentage : "))
r=input("Enter Remarks: ")
writecsv.writerow([rl,n,p,r])
print(" Data saved in Student Details file..")
choice=input("Want add more record(y/n).....")
if arr[mid] == x:
return mid
if arr[mid] == x:
return mid
else:
else:
return -1
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
# Function call
if result != -1:
else:
if n <= 1:
return # If the array has 0 or 1 element, it is already sorted, so return
for i in range(1, n): # Iterate over the array starting from the second element
key = arr[i] # Store the current element as the key to be inserted in the right
position
j = i-1
while j >= 0 and key < arr[j]: # Move elements greater than key one position
ahead
arr[j+1] = arr[j] # Shift elements to the right
j -= 1
arr[j+1] = key # Insert the key in the correct position
# Example usage:
arr = [10, 23, 45, 70, 11, 15]
target = 70
# Function call
result = linear_search(arr, target)
if result != -1:
print(f"Element found at index: {result}")
else:
print("Element not found in the array")
OUTPUT 11
PROGRAM 12
12. Write a python program to find largest
element in an Array.
def largest(arr, n):
# Driver Code
arr = [10, 324, 45, 90, 9808]
n = len(arr)
Ans = largest(arr, n)
print("Largest in given array ", Ans)
OUTPUT 12
PROGRAM 13
13. Write a program to check whether the
number is armstrong number or not.
str_lst =list(str)
# Iterate list
for i in range(len(str_lst)):
if str_lst[i] in 'aeiouAEIOU':
str_lst[i]='*'
new_str = "".join(str_lst)
return new_str
def main():
line = input("Enter string: ")
print("Orginal String")
print(line)
print("After replacing Vowels with '*'")
print(strep(line))
main()
OUTPUT 14
PROGRAM 15
def lstSum(lst,n):
if n==0:
return 0
else:
return lst[n-1]+lstSum(lst,n-1)