0% found this document useful (0 votes)
4 views

Python_Programs_with_Outputs

The document contains multiple Python programs demonstrating various functionalities including a simple calculator, finding square roots, calculating the area of a triangle, converting Celsius to Fahrenheit, checking if a number is positive, negative, or zero, and determining if a number is odd or even. It also includes programs for calculating grades based on percentage, generating natural numbers, printing even numbers within a range, summing natural numbers, printing list elements, finding numbers divisible by 7 and multiples of 5, and checking if a number is prime. Each program is accompanied by its respective code and output instructions.

Uploaded by

kelovi9974
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python_Programs_with_Outputs

The document contains multiple Python programs demonstrating various functionalities including a simple calculator, finding square roots, calculating the area of a triangle, converting Celsius to Fahrenheit, checking if a number is positive, negative, or zero, and determining if a number is odd or even. It also includes programs for calculating grades based on percentage, generating natural numbers, printing even numbers within a range, summing natural numbers, printing list elements, finding numbers divisible by 7 and multiples of 5, and checking if a number is prime. Each program is accompanied by its respective code and output instructions.

Uploaded by

kelovi9974
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Programs with Outputs

Write a program to make a Simple Calculator

# Simple Calculator
def calculator():
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

choice = input("Enter choice (1/2/3/4): ")

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


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

if choice == '1':
print(f"The result is: {num1 + num2}")
elif choice == '2':
print(f"The result is: {num1 - num2}")
elif choice == '3':
print(f"The result is: {num1 * num2}")
elif choice == '4':
if num2 != 0:
print(f"The result is: {num1 / num2}")
else:
print("Error: Division by zero is not allowed.")
else:
print("Invalid Input")

calculator()

Write a program to find the Square Root

# Find Square Root


import math

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


if number >= 0:
print(f"The square root of {number} is {math.sqrt(number)}")
else:
print("Square root of negative number is not real.")

Write a program to Calculate the Area of a Triangle

# Area of a Triangle
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = 0.5 * base * height
print(f"The area of the triangle is {area}")

Write a program to Convert Celsius to Fahrenheit

# Convert Celsius to Fahrenheit


celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius} Celsius is equal to {fahrenheit} Fahrenheit")

Write a program to Check if a Number is Positive, Negative or 0

# Check if Number is Positive, Negative, or Zero


number = float(input("Enter a number: "))
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")

Write a program to Check if a Number is Odd or Even

# Check if Number is Odd or Even


number = int(input("Enter a number: "))
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
Write a program to find the grade of the student on the basis of percentage

# Calculate Grade Based on Percentage


percentage = float(input("Enter percentage: "))
if percentage >= 90:
grade = "A"
elif percentage >= 80:
grade = "B"
elif percentage >= 70:
grade = "C"
elif percentage >= 60:
grade = "D"
elif percentage >= 40:
grade = "E"
else:
grade = "F"

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

Write a program to generate natural numbers between 1 and 10

# Generate Natural Numbers from 1 to 10


for i in range(1, 11):
print(i)

Write a program to print even numbers between 10 and 30

# Print Even Numbers Between 10 and 30


for i in range(10, 31):
if i % 2 == 0:
print(i)

Write a program to find the Sum of Natural Numbers

# Sum of Natural Numbers


n = int(input("Enter a positive number: "))
if n > 0:
total = n * (n + 1) // 2
print(f"The sum of first {n} natural numbers is {total}")
else:
print("Enter a positive number.")

Write a program to print all elements in a list using a for loop

# Print List Elements


countries = ["INDIA", "NEPAL", "SRILANKA", "BANGLADESH"]
for country in countries:
print(country)

Write a program to find numbers divisible by 7 and multiples of 5 between 1200


and 2200

# Numbers Divisible by 7 and Multiple of 5


for i in range(1200, 2201):
if i % 7 == 0 and i % 5 == 0:
print(i)

Write a program to check whether a number is prime or not using 'while' loop

# Check if a Number is Prime


number = int(input("Enter a number: "))
if number > 1:
i=2
is_prime = True
while i <= number // 2:
if number % i == 0:
is_prime = False
break
i += 1
if is_prime:
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")
else:
print(f"{number} is not a prime number.")

You might also like