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

Cs HHW

Uploaded by

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

Cs HHW

Uploaded by

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

1.

False
2. (b)
3. (d)
4. (d)
5. (c)
6. (d)
7. (b)
8. C#l#r helps t# give shad#w t# the picture
9. (a)(d)
10.
11.
12. 200#12#
13. eF
eD
cD
cB
aB

14. fUNnpYTHONn3.7.

15.

16. import math

def simpleInterest(p, r, t):


return (p * r * t) / 100

def areaTriangle(a, b, c):


s = (a + b + c) / 2
return math.sqrt(s * (s - a) * (s - b) * (s - c))

def largest(a, b, c):


return max(a, b, c)

def sumNaturalNum(n):
return (n * (n + 1)) // 2

def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)

def sumIndividual(n):
return sum(int(digit) for digit in str(n))

def reverseNum(n):
return int(str(n)[::-1])

def hcf(dividend, divisor):


while divisor != 0:
dividend, divisor = divisor, dividend % divisor
return dividend

def checkPrime(n):
if n <= 1:
return False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True

def menu():
print("Menu:")
print("1. Compute Simple Interest")
print("2. Area of a Triangle using Heron's Formula")
print("3. Find the Largest of Three Numbers")
print("4. Find the Sum of n Natural Numbers")
print("5. Find the Factorial of a Number")
print("6. Find the Sum of Individual Digits of a Number")
print("7. Find the Reverse of a Number")
print("8. Find the HCF of Two Numbers")
print("9. Check for Prime Number")
print("10. Exit")

choice = int(input("Enter your choice: "))


return choice

def main():
while True:
choice = menu()

if choice == 1:
p = float(input("Enter principal amount: "))
r = float(input("Enter rate of interest: "))
t = float(input("Enter time period in years: "))
print("Simple Interest:", simpleInterest(p, r, t))

elif choice == 2:
a = float(input("Enter side a: "))
b = float(input("Enter side b: "))
c = float(input("Enter side c: "))
print("Area of Triangle:", areaTriangle(a, b, c))

elif choice == 3:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
print("Largest number:", largest(a, b, c))

elif choice == 4:
n = int(input("Enter a positive integer: "))
print("Sum of natural numbers:", sumNaturalNum(n))

elif choice == 5:
n = int(input("Enter a positive integer: "))
print("Factorial:", factorial(n))

elif choice == 6:
n = int(input("Enter a positive integer: "))
print("Sum of individual digits:", sumIndividual(n))

elif choice == 7:
n = int(input("Enter a positive integer: "))
print("Reverse of the number:", reverseNum(n))

elif choice == 8:
dividend = int(input("Enter the first number: "))
divisor = int(input("Enter the second number: "))
print("HCF:", hcf(dividend, divisor))

elif choice == 9:
n = int(input("Enter a positive integer: "))
if checkPrime(n):
print(f"{n} is a prime number.")
else:
print(f"{n} is not a prime number.")

elif choice == 10:


print("Exiting the program.")
break

else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()

17.

18. def count_even_odd(numbers):


even_count = 0
odd_count = 0
for num in numbers:
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
return even_count, odd_count

# Input numbers from the user


n = input("Enter numbers seperated by comma(,) : ")
a = tuple(int(x) for x in n.split(","))

# Call the function and print the result


even, odd = count_even_odd(a)
print("Number of even numbers:", even)
print("Number of odd numbers:", odd)

19. def count_occurrences(dictionary, lst):


for item in lst:
if item in dictionary:
dictionary[item] += 1
else:
dictionary[item] = 1
return dictionary

# Initialize an empty dictionary


frequency_dict = {}

# Input numbers from the user for the list


n = input("Enter the number of elements in the list: ")
lst = list(int(x) for x in n.split(","))

# Call the function to count occurrences and update the dictionary


updated_dict = count_occurrences(frequency_dict, lst)

# Print the updated dictionary


print("Updated Dictionary with frequencies:", updated_dict)

20.

21. def sum_of_values_ending_with_3(lst):


total = 0
for num in lst:
if isinstance(num, int) and str(num).endswith('3'):
total += num
return total

# Input numbers from the user to form the list


n = input("Enter the number of elements in the list: ")
lst = list(int(x) for x in n.split(","))

# Call the function and print the result


result = sum_of_values_ending_with_3(lst)
print("Sum of all values ending with 3:", result)

22.

23. def capitalize_words(input_string):


# Split the string into a list of words
words = input_string.split()
# Capitalize the first letter of each word
capitalized_words = [word.capitalize() for word in words]
# Join the capitalized words back into a single string
capitalized_string = ' '.join(capitalized_words)
return capitalized_string

# Input string from the user


input_string = input("Enter a string: ")

# Call the function and print the result


result = capitalize_words(input_string)
print("Capitalized string:", result)

24. def process_list(lst):


processed_list = []
for num in lst:
if num % 2 == 0:
processed_list.append(num / 2)
else:
processed_list.append(num * 2)
return processed_list
# Input numbers from the user to form the list
n = input("Enter the number of elements in the list: ")
lst = list(int(x) for x in n.split(","))

# Call the function and print the result


result = process_list(lst)
print("Processed list:", result)

25. def count_character(input_string, search_char):


count = 0
for char in input_string:
if char == search_char:
count += 1
return count

# Input string from the user


input_string = input("Enter a string: ")

# Input the character to search for


search_char = input("Enter the character to search for: ")

# Call the function and print the result


result = count_character(input_string, search_char)
print(f"The character '{search_char}' appears {result} times in the string.")

You might also like