You are on page 1of 7

1.Take character as input from the console using input() function.

Write a program to check


whether the given input is a character or a digit, if the input is 0 exit the program, otherwise
print the result.

Ans =

while True:

user_input = input("Enter a character or digit: ")

if user_input == '0':

print("Exiting the program...")

break

elif user_input.isalpha():

print("The input is a character.")

elif user_input.isdigit():

print("The input is a digit.")

else:

print("The input is neither a character nor a digit.")

2. Write a Python program to count the vowels and consonants in the string, which is
inputted from the user.

Ans =

# take input from the user

string = input("Enter a string: ")

# initialize counters for vowels and consonants

vowel_count = 0

consonant_count = 0

# iterate through each character in the string

for char in string:

# check if the character is a vowel

if char.lower() in 'aeiou':

vowel_count += 1

# check if the character is a consonant


elif char.isalpha():

consonant_count += 1

# print the results

print("Number of vowels:", vowel_count)

print("Number of consonants:", consonant_count)

3. Write a Python program to convert Fahrenheit into Celsius. The formula of Fahrenheit
conversion into celsius is: C= ((fahrenheit-32)*5)/9. Print the result.

Ans =

# take input from the user

fahrenheit = float(input("Enter temperature in Fahrenheit: "))

# convert Fahrenheit to Celsius using the formula

celsius = ((fahrenheit - 32) * 5) / 9

# print the result

print("Temperature in Celsius:", celsius)

4. Write a Python program to calculate Factorial of user input data by using the recursion concept.
Print the result.

Ans =

# define the factorial function using recursion

def factorial(n):

if n == 0:

return 1

else:

return n * factorial(n-1)

# take input from the user

num = int(input("Enter a positive integer: "))


# check if the input is positive

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", factorial(num))

5. Write a program in Python to check whether an integer Armstrong number or not

Ans =

# define a function to check if a number is an Armstrong number

def is_armstrong(num):

# convert the number to a string to count the number of digits

num_str = str(num)

num_digits = len(num_str)

# calculate the sum of the cubes of the digits

sum = 0

for digit in num_str:

sum += int(digit) ** num_digits

# check if the sum is equal to the original number

if sum == num:

return True

else:

return False
# take input from the user

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

# check if the number is an Armstrong number and print the result

if is_armstrong(num):

print(num, "is an Armstrong number")

else:

print(num, "is not an Armstrong number")

6. write a python program to enter 3*3 matrix from the console and also print transpose of it

Ans =

# take input from the user for the matrix

matrix = []

for i in range(3):

row = input("Enter row " + str(i+1) + " (separate the numbers with spaces): ")

matrix.append([int(num) for num in row.split()])

# print the original matrix

print("Original matrix:")

for row in matrix:

print(row)

# transpose the matrix

transpose = []

for i in range(3):

row = []

for j in range(3):

row.append(matrix[j][i])

transpose.append(row)
# print the transpose of the matrix

print("Transpose of the matrix:")

for row in transpose:

print(row)

7. Let's write a simple python code for function.

The program has a function sayhello() It takes a single argument which is a string.

The argument inside the parenthesis of the function is username We end the header with a colon:

Inside the function create a variable greet and assign a value "Hello"

Concatenate the variable with username and print the result.

Let us look at the reusability of the function. Let us create a list of names to whom we want to
print the greeting

L1 [Ram', 'Mahesh', Vasudha', 'Uma', 'Sekhar, John"] Let's call the function by passing the items of
the list as argument and print the result

Ans = def sayhello(username):

greet = "Hello"

print(greet + ", " + username)

# list of names

L1 = ['Ram', 'Mahesh', 'Vasudha', 'Uma', 'Sekhar', 'John']

# calling the function for each name in the list

for name in L1:

sayhello(name)

8. write a python program to check whether the user input integer is leap year is not.

Ans=

# take input from the user

year = int(input("Enter a year: "))

# check if the year is a leap year or not


if year % 4 == 0:

if year % 100 == 0:

if year % 400 == 0:

print(year, "is a leap year")

else:

print(year, "is not a leap year")

else:

print(year, "is a leap year")

else:

print(year, "is not a leap year")

9. write a python program to find GCD of two user input number.

Ans=

# take input from the user

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

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

# define a function to find GCD of two numbers using Euclid's algorithm

def gcd(a, b):

if b == 0:

return a

else:

return gcd(b, a % b)

# calculate the GCD of the two numbers using the defined function

result = gcd(num1, num2)

# print the GCD

print("The GCD of", num1, "and", num2, "is", result)


10. Define a class Person.
Add a method setName() which takes self and name as parameters. Inside the method, set
self.name= name.
Add a method getName() which takes self as parameter.
Inside the method retum self.name. Create two instances of the class Person p1 and p2.
Take the input name from the user. Call the method setName() on p1 and pass name as
parameter. Take the input name from the user.
Call the method setName() on p2 and pass name as parameter.
Call the method getName() on p1.
Call the method getName() on p2.

Ans=

class Person:

def setName(self, name):

self.name = name

def getName(self):

return self.name

p1 = Person()

p2 = Person()

name1 = input("Enter name for p1: ")

p1.setName(name1)

name2 = input("Enter name for p2: ")

p2.setName(name2)

print("Name of p1:", p1.getName())

print("Name of p2:", p2.getName())

You might also like