0% found this document useful (0 votes)
26 views2 pages

Sample Programs

The document provides a series of Python programs demonstrating basic programming concepts, including printing messages, arithmetic operations, conditional statements, and user input handling. Key examples include calculating the sum of two numbers, checking if a number is even or odd, calculating factorials, and finding the area of a circle. Each program is presented with code snippets and brief descriptions of their functionality.

Uploaded by

simplybotid6969
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)
26 views2 pages

Sample Programs

The document provides a series of Python programs demonstrating basic programming concepts, including printing messages, arithmetic operations, conditional statements, and user input handling. Key examples include calculating the sum of two numbers, checking if a number is even or odd, calculating factorials, and finding the area of a circle. Each program is presented with code snippets and brief descriptions of their functionality.

Uploaded by

simplybotid6969
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. First program to print a simple message.

Program:
print("Hello, World!")

2. Adding Two Numbers: Takes two numbers as input and prints their sum.
Program:
num1 = float(input("Enter first number: "))

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

sum_result = num1 + num2

print("The sum of two numbers:",sum_result)

3. Checking if a Number is Even or Odd:


Program:
number = 7
if number % 2 == 0:
print(number, "is an even number.")
else:
print(number, "is an odd number.")

4. Calculating Factorial of a Number:


num = 5
factorial = 1
for i in range(1, num + 1):
factorial *= i
print("The factorial of", num, "is", factorial)

5. Maximum of Two numbers


a=7
b=3
print(max(a, b))

6. Find Simple Interest in Python Using User Input

# Taking user input for principal, rate, and time


p = float(input("Enter principal amount: "))
r = float(input("Enter rate of interest: "))
t = float(input("Enter time in years: "))

# Calculating simple interest using formula


si = (p * r * t) / 100

# Printing the result


print("Simple Interest:", si)
7. Average of Three Numbers
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
print("Average:", (a + b + c) / 3) # Calculate and print the average

8. Check if a number is positive or negative


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

# Condition checking

# if the number is greater than 0, then positive


# else if the number is lesser than 0, then negative
# else number is 0
if number>0:
print(number, "is positive.")
elif number<0:
print(number, "is negative.")
else:
print("Number is 0.")

9. Swap Two Variables

# Taking user input


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

# Printing before swap


print(f"Before swap: num1 = {num1} num2 = {num2}")

# Swapping
temp = num1
num1 = num2
num2 = temp

# Printing after swap


print(f"After swap: num1 = {num1} num2 = {num2}")

10. Area of Circle

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


area 3.14 * radius * radius # Area formula: π * r^2

print("Area of the circle:", area)

You might also like