A.I. Practical
A.I. Practical
Q.2) W.A.P to count the numbers of even and odd numbers ranging from 0 to 20 using for loop.
Program - even_count = 0
odd_count = 0
for num in range(21):
if num % 2 == 0: # Check if the number is even
even_count += 1
else:
odd_count += 1
print("Count of even numbers:", even_count)
print("Count of odd numbers:", odd_count)
Q.3) W.A.P to calculate the factorial of a positive number using for loop.
Program - num = int(input("Enter a positive number: "))
factorial = 1
if num < 0:
print("Factorial is not defined for negative numbers.")
elif num == 0:
print("The factorial of 0 is 1.")
else:
for i in range(1, num + 1):
factorial *= i
print(f"The factorial of {num} is {factorial}")
Q.4) W.A.P. to accept a number from the user and print the table of it.
Program - num = int(input("Enter a number: "))
print(f"Multiplication table for {num}:")
for i in range(1, 11):
result = num * i
print(f"{num} x {i} = {result}")
Q.5) W.A.P. to calculate the sum of numbers from 1 to 10 using for loop.
Program - sum_of_numbers = 0
for num in range(1, 11):
sum_of_numbers += num
print("The sum of numbers from 1 to 10 is:", sum_of_numbers)
Q.6) W.A.P. to accept a number from the user and print how many digits are there in the given
numbers using while loop.
Program -
number = int(input("Enter a number: "))
digit_count = 0
if number == 0:
digit_count = 1
while number != 0:
number = number // 10 # Remove the last digit
digit_count += 1
print(f"The number has {digit_count} digit(s).")
Q.7) W.A.P. to input a range from the user and count the number of odd numbers in the range
using while loop.
Program - start = int(input("Enter the starting number of the range: "))
end = int(input("Enter the ending number of the range: "))
odd_count = 0
current = start
while current <= end:
if current % 2 != 0:
odd_count += 1
current += 1
print(f"There are {odd_count} odd number(s) in the range from {start} to {end}.")
Q.8) W.A.P. to add two elements of the list.
Program - numbers = [5, 8, 12, 3, 7]
index1 = int(input("Enter the index of the first element: "))
index2 = int(input("Enter the index of the second element: "))
if 0 <= index1 < len(numbers) and 0 <= index2 < len(numbers):
result = numbers[index1] + numbers[index2]
print(f"The sum of elements at index {index1} and index {index2} is: {result}")
else:
print("Invalid indices. Make sure the indices are within the range of the list.")
Q.9) W.A.P. to print squares of all numbers present in a list [8 , 2 , 7 , 11 , 20 ,6].
Program - numbers = [8, 2, 7, 11, 20, 6]
for num in numbers:
square = num ** 2
print(f"The square of {num} is: {square}")
Q.10) W.A.P. to print numbers divisible by 5 from a given list [11 , 25 , 35 , 33 , 46].
Program - numbers = [11, 25, 35, 33, 46]
print("Numbers divisible by 5:")
for num in numbers:
if num % 5 == 0:
print(num)
Q.11) W.A.P. to calculate total and average marks from the marks of different subjects stored in
the form of list.
Program - marks = [85, 90, 78, 92, 88]
total_marks = sum(marks)
average_marks = total_marks / len(marks)
print(f"Total marks: {total_marks}")
print(f"Average marks: {average_marks}")
Q.12) W.A.P. that accepts a character from the user and check whether the entered character is a
vowel or a consonant.
Program - char = input("Enter a character: ")
if len(char) == 1:
char = char.lower()
if char in "aeiou":
print(f"{char} is a vowel.")
else:
print(f"{char} is a consonant.")
else:
print("Please enter a single character.")
Q.13) W.A.P. that accepts a string from the user and calculate the numbers of digits and letters.
Program - user_input = input("Enter a string: ")
digit_count = 0
letter_count = 0
for char in user_input:
if char.isdigit():
digit_count += 1
elif char.isalpha():
letter_count += 1
print(f"Number of digits: {digit_count}")
print(f"Number of letters: {letter_count}")
Q.14) W.A.P. that accepts a string and check whether it contains alphanumeric character or not.
Program - user_input = input("Enter a string: ")
contains_alphanumeric = False
for char in user_input:
if char.isalnum():
contains_alphanumeric = True
break
if contains_alphanumeric:
print("The string contains at least one alphanumeric character.")
else:
print("The string does not contain any alphanumeric characters.")
Q.15) W.A.P. to print squares of first 5 natural numbers using while loop.
Program - num = 1
count = 0
while count < 5:
square = num ** 2
print(f"The square of {num} is: {square}")
num += 1
count += 1
Q.16) W.A.P. to input a day number from 1 to 7 and display day name as Sunday for 1 and
Monday for 2 and so on.
Program - day_number = int(input("Enter a day number (1-7): "))
if day_number == 1:
day_name = "Sunday"
elif day_number == 2:
day_name = "Monday"
elif day_number == 3:
day_name = "Tuesday"
elif day_number == 4:
day_name = "Wednesday"
elif day_number == 5:
day_name = "Thursday"
elif day_number == 6:
day_name = "Friday"
elif day_number == 7:
day_name = "Saturday"
else:
day_name = "Invalid day number"
print(f"The day corresponding to day number {day_number} is {day_name}.")
Q.17) W.A.P. to input a number and check the following if the number is - 1)Even and <50
2)Even and >50 3) Even and = 50 4)Odd.
Program - number = int(input("Enter a number: "))
if number % 2 == 0:
if number < 50:
print("The number is even and less than 50.")
elif number > 50:
print("The number is even and greater than 50.")
else:
print("The number is even and equal to 50.")
else:
print("The number is odd.")