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

Python Programs

The document contains a series of Python code snippets demonstrating basic programming concepts such as finding absolute values, checking even/odd status, comparing numbers, determining prime status, calculating summation, checking divisibility, and implementing a simple calculator. Each snippet prompts user input and provides corresponding outputs based on the calculations or checks performed. Additionally, there is an infinite loop that prints 'good' indefinitely.

Uploaded by

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

Python Programs

The document contains a series of Python code snippets demonstrating basic programming concepts such as finding absolute values, checking even/odd status, comparing numbers, determining prime status, calculating summation, checking divisibility, and implementing a simple calculator. Each snippet prompts user input and provides corresponding outputs based on the calculations or checks performed. Additionally, there is an infinite loop that prints 'good' indefinitely.

Uploaded by

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

1.

Find the Absolute Value of a Given Number


number = float(input("Enter a number: "))# Prompt the user to input a number
# Calculate the absolute value manually
if number < 0:
absolute_value = -number
else:
absolute_value = number
print("The absolute value is:", absolute_value) # Print the result

2. Check if a Number is Even or Odd


number = int(input("Enter a number: "))
if number % 2 == 0:
print("The number is Even.")
else:
print("The number is Odd.")

3. Compare Two Numbers


number1 = float(input("Enter the first number: "))
number2 = float(input("Enter the second number: "))

if number1 < number2:


print("The first number is Smaller than the second number.")
elif number1 > number2:
print("The first number is Greater than the second number.")
else:
print("The first number is Equal to the second number.")

4. Check if a Number is Prime


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

if number <= 1:
print(f"{number} is not a prime number.")
else:
is_prime = True
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
is_prime = False
break
if is_prime:
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")

5. Find the Summation of a Series: 1 + 2 + 3 + ... + n

n = int(input("Enter a number: ")) # Initialize the summation variable


summation = 0
for i in range(1, n + 1): # Calculate the summation using a loop
summation += i
print("The summation is:", summation) # Print the result

6. Check if a Given Number is Divisible by 3


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

if number % 3 == 0:
print(f"{number} is divisible by 3.")
else:
print(f"{number} is not divisible by 3.")

7. Print “good” Indefinitely


while True:
print("good")

8. Calculator Function (Add, Subtract, Multiply, Divide)


number1 = float(input("Enter the first number: "))
number2 = float(input("Enter the second number: "))
operation = input("Enter the operation (add, subtract, multiply, divide):
").lower()

if operation == 'add':
result = number1 + number2
elif operation == 'subtract':
result = number1 - number2
elif operation == 'multiply':
result = number1 * number2
elif operation == 'divide':
if number2 == 0:
result = "Cannot divide by zero!"
else:
result = number1 / number2
else:
result = "Invalid operation"

print("The result is:", result)

You might also like