0% found this document useful (0 votes)
8 views9 pages

Untitled Document-2

The document contains a series of programming exercises in Python, covering various topics such as greeting messages, arithmetic operations, voting eligibility, number comparisons, and geometric calculations. Each exercise includes a description, code implementation, and example outputs to illustrate the expected functionality. The exercises are designed to help users practice and understand basic programming concepts.

Uploaded by

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

Untitled Document-2

The document contains a series of programming exercises in Python, covering various topics such as greeting messages, arithmetic operations, voting eligibility, number comparisons, and geometric calculations. Each exercise includes a description, code implementation, and example outputs to illustrate the expected functionality. The exercises are designed to help users practice and understand basic programming concepts.

Uploaded by

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

Q1.

Write a program to take your name as input and print a greeting


message like, "Hello, [Your Name]! Welcome to Python Programming.​

# Program to display a greeting message

# Taking name as input from the user


name = input("Enter your name: ")

# Displaying the greeting message


print(f"Hello, {name}! Welcome to Python Programming.")

Example Output​
Enter your name: Vikas Chhabra Hello, Vikas Chhabra! Welcome to Python Programming.​

Q2. Product of Two Numbers: Write a program to input two numbers from the user and
print
their product

# Program to calculate the product of two numbers

# Taking two numbers as input from the user


num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Calculating the product


product = num1 * num2

# Displaying the result


print(f"The product of {num1} and {num2} is {product}.")

Example Output​
Enter the first number: 8 Enter the second number: 5 The product of 8.0 and 5.0 is 40.0.​

Q3 Simple Calculator: Create a program that takes two numbers and an operator (+, -, *, /)
as
input and performs the respective calculation​

# Program for a simple calculator

# Taking input from the user


num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
operator = input("Enter an operator (+, -, *, /): ")
# Performing the respective calculation based on the operator
if operator == '+':
​ result = num1 + num2
​ print(f"The result of {num1} + {num2} is {result}")
elif operator == '-':
​ result = num1 - num2
​ print(f"The result of {num1} - {num2} is {result}")
elif operator == '*':
​ result = num1 * num2
​ print(f"The result of {num1} * {num2} is {result}")
elif operator == '/':
​ if num2 != 0:
​ result = num1 / num2
​ print(f"The result of {num1} / {num2} is {result}")
​ else:
​ print("Error: Division by zero is not allowed!")
else:
​ print("Invalid operator! Please use +, -, *, or /.")

Example Output​
Enter the first number: 12
Enter the second number: 4
Enter an operator (+, -, *, /): /
The result of 12.0 / 4.0 is 3.0

Q4 Voting Eligibility: Write a program to input a person’s age and check if they are
eligible to
vote (age >= 18)​
# Program to check voting eligibility

# Taking age as input from the user


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

# Checking eligibility
if age >= 18:
​ print("You are eligible to vote!")
else:
​ print("Sorry, you are not eligible to vote yet.")

Example Output
Enter your age: 20 You are eligible to vote!​
Enter your age: 16 Sorry, you are not eligible to vote yet.
Q5 Odd or Even: Write a program to check whether a given number is odd or even.​

# Program to check if a number is odd or even

# Taking a number as input from the user


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

# Checking if the number is odd or even


if number % 2 == 0:
​ print(f"The number {number} is even.")
else:
​ print(f"The number {number} is odd.")

Example Output​
Enter a number: 8 The number 8 is even.​
Enter a number: 7 The number 7 is odd.

Q6. Greatest of Two Numbers: Write a program to input two numbers and print the
greater
Number.​
# Program to find the greatest of two numbers

# Taking two numbers as input from the user


num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Checking which number is greater


if num1 > num2:
​ print(f"The greater number is {num1}.")
elif num2 > num1:
​ print(f"The greater number is {num2}.")
else:
​ print("Both numbers are equal.")

Example Output​
Enter the first number: 15 Enter the second number: 10 The greater number is 15.0.​
Enter the first number: 8 Enter the second number: 12 The greater number is 12.0.​
Enter the first number: 7 Enter the second number: 7 Both numbers are equal.

Q7Pass or Fail: Create a program to input marks of a student. If the marks are 33 or more,
print
"Pass"; otherwise, print "Fail."​
# Program to check if a student passes or fails
# Taking marks as input from the user
marks = float(input("Enter your marks: "))

# Checking if the student passes or fails


if marks >= 33:
​ print("Pass")
else:
​ print("Fail")
Example Output
Enter your marks: 45
Pass

Enter your marks: 25


Fail

Q8 Number Comparison: Write a program to input three numbers and find the largest
number
among them.​
# Program to find the largest of three numbers

# Taking three numbers as input from the user


num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

# Finding the largest number


if num1 >= num2 and num1 >= num3:
​ print(f"The largest number is {num1}.")
elif num2 >= num1 and num2 >= num3:
​ print(f"The largest number is {num2}.")
else:
​ print(f"The largest number is {num3}.")

Example Output​
Enter the first number: 10
Enter the second number: 5
Enter the third number: 8
The largest number is 10.0.​

Enter the first number: 4


Enter the second number: 9
Enter the third number: 6
The largest number is 9.0.
Enter the first number: 3
Enter the second number: 7
Enter the third number: 10
The largest number is 10.0.

Q9Leap Year Check: Write a program to input a year and check whether it is a leap year​
# Program to check if a year is a leap year

# Taking year as input from the user


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

# Checking if the year is a leap year


if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
​ print(f"{year} is a leap year.")
else:
​ print(f"{year} is not a leap year.")

Example Output​

Enter a year: 2024
2024 is a leap year.​

Enter a year: 2023


2023 is not a leap year.

Q10 Grade Assignment: Create a program to assign grades based on marks:


• Marks >= 90: Grade A
• Marks >= 75 and < 90: Grade B
• Marks >= 50 and < 75: Grade C
• Marks < 50: Grade D​

# Program to assign grades based on marks

# Taking marks as input from the user


marks = float(input("Enter your marks: "))

# Assigning grades based on the marks


if marks >= 90:
​ grade = 'A'
elif marks >= 75:
​ grade = 'B'
elif marks >= 50:
​ grade = 'C'
else:
​ grade = 'D'

# Displaying the grade


print(f"Your grade is: {grade}")

Example Output​
Enter your marks: 92
Your grade is: A

Enter your marks: 80


Your grade is: B

Enter your marks: 65


Your grade is: C

Enter your marks: 45


Your grade is: D

Q11. Area of a Circle: Write a program to input the radius of a circle and calculate its
area. Use
the formula:
Area=π×radius2
(Take π=3.14).​

# Program to calculate the area of a circle

# Taking radius as input from the user


radius = float(input("Enter the radius of the circle: "))

# Defining the value of pi


pi = 3.14

# Calculating the area of the circle


area = pi * (radius ** 2)

# Displaying the area


print(f"The area of the circle with radius {radius} is {area}.")​
Example Output​
Enter the radius of the circle: 5
The area of the circle with radius 5.0 is 78.5.

Q12 Area of a Rectangle: Write a program to input the length and breadth of a rectangle
and calculate its area.​

# Program to calculate the area of a rectangle

# Taking length and breadth as input from the user


length = float(input("Enter the length of the rectangle: "))
breadth = float(input("Enter the breadth of the rectangle: "))

# Calculating the area of the rectangle


area = length * breadth

# Displaying the area


print(f"The area of the rectangle with length {length} and breadth {breadth} is {area}.")

Example Output​
Enter the length of the rectangle: 8
Enter the breadth of the rectangle: 5
The area of the rectangle with length 8.0 and breadth 5.0 is 40.0.

Q13 Perimeter of a Rectangle: Write a program to input the length and breadth of a
rectangle
and calculate its perimeter.

# Program to calculate the perimeter of a rectangle

# Taking length and breadth as input from the user


length = float(input("Enter the length of the rectangle: "))
breadth = float(input("Enter the breadth of the rectangle: "))

# Calculating the perimeter of the rectangle using the formula


perimeter = 2 * (length + breadth)

# Displaying the perimeter


print(f"The perimeter of the rectangle with length {length} and breadth {breadth} is {perimeter}.")​

Example Output​
Enter the length of the rectangle: 10
Enter the breadth of the rectangle: 5
The perimeter of the rectangle with length 10.0 and breadth 5.0 is 30.0.

Q14 Average Calculation: Write a program to input three numbers and calculate their
average​

# Program to calculate the average of three numbers
# Taking three numbers as input from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

# Calculating the average


average = (num1 + num2 + num3) / 3

# Displaying the average


print(f"The average of {num1}, {num2}, and {num3} is {average}.")

Example Output​
Enter the first number: 8
Enter the second number: 10
Enter the third number: 12
The average of 8.0, 10.0, and 12.0 is 10.0.

Q15 Eligibility for Rewards: A shop gives rewards if a customer spends more than ₹5000.
Write a program to take input of the amount spent and check if the customer is eligible
for rewards or not.​
# Program to check eligibility for rewards

# Taking the amount spent as input from the user


amount_spent = float(input("Enter the amount spent by the customer: "))

# Checking if the customer is eligible for rewards


if amount_spent > 5000:
​ print("You are eligible for rewards!")
else:
​ print("You are not eligible for rewards.")

Example Output​

Enter the amount spent by the customer: 6000
You are eligible for rewards!

Enter the amount spent by the customer: 4500


You are not eligible for rewards.

You might also like