0% found this document useful (0 votes)
1 views16 pages

15 Python Programs

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)
1 views16 pages

15 Python Programs

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

code

15 Python Programs
Submitted by: Arjun Syam 10-B
Adding Two Numbers

add_circle A simple program to add two numbers and display the result

# Program to add two numbers


num1 = 5
num2 = 7
sum = num1 + num2
print("The sum of", num1, "and", num2, "is", sum)
Finding the Area of a Rectangle

crop_square A simple program to calculate the area of a rectangle

# Program to find the area of a rectangle


length = 10
width = 5
area = length * width
print("The area of the rectangle is", area)
Converting Celsius to Fahrenheit

thermostat A simple program to convert temperature from Celsius to Fahrenheit

# Program to convert Celsius to Fahrenheit


celsius = 25
fahrenheit = (celsius * 9/5) + 32
print(celsius, "°C is equal to", fahrenheit, "°F")
Checking if a Number is Even or Odd

filter_2 A simple program to determine if a number is even or odd

# Program to check if a number is even or odd


number = 10
if number % 2 == 0:
print(number, "is even")
else:
print(number, "is odd")
Finding the Factorial of a Number

functions A simple program to calculate the factorial of a number

# Program to find the factorial of a number


number = 5
factorial = 1
for i in range(1, number + 1):
factorial = factorial * i
print("The factorial of", number, "is", factorial)
Finding the Greatest of Two Numbers

compare_arrows A simple program to determine which of two numbers is greater

# Program to find the greatest of two numbers


num1 = 15
num2 = 20
if num1 > num2:
print(num1, "is greater than", num2)
else:
print(num2, "is greater than", num1)
Simple Calculator

calculate A simple program to perform basic arithmetic operations

# Simple calculator program


num1 = 10
num2 = 5
print("Addition:", num1 + num2)
print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)
print("Division:", num1 / num2)
Printing a Pattern

grid_on A simple program to print a right-angled triangle pattern

# Program to print a pattern


for i in range(1, 6):
for j in range(1, i + 1):
print("*", end=" ")
print()

*
* *
* * *
* * * *
* * * * *
Finding the Sum of Digits of a Number

add_circle_outline A simple program to calculate the sum of digits in a number

# Program to find the sum of digits of a number


number = 1234
sum = 0
while number > 0:
digit = number % 10
sum = sum + digit
number = number // 10
print("The sum of digits is", sum)

Example: For number 1234, the sum of digits is 1 + 2 + 3 + 4 = 10


Checking if a Number is Prime

filter_list A simple program to determine if a number is prime

# Program to check if a number is prime


number = 13
is_prime = True
for i in range(2, number):
if number % i == 0:
is_prime = False
break
if is_prime:
print(number, "is a prime number")
else:
print(number, "is not a prime number")

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Finding the Square of a Number

exponent A simple program to calculate the square of a number

# Program to find the square of a number


number = 8
square = number ** 2
print("The square of", number, "is", square)

The square of a number is the result of multiplying the number by itself.


Formula: square = number²
In Python, we use the exponentiation operator (**)

8 × 8 = 64
Converting Hours to Minutes

schedule A simple program to convert hours to minutes

# Program to convert hours to minutes


hours = 3
minutes = hours * 60
print(hours, "hours is equal to", minutes, "minutes")

access_time timer
Conversion Formula:

3 hours
arrow_forward 180 minutes
minutes = hours × 60
1 hour = 60 minutes
Finding the Average of Numbers

calculate A simple program to calculate the average of numbers

# Program to find the average of numbers


num1 = 10
num2 = 20
num3 = 30
average = (num1 + num2 + num3) / 3
print("The average is", average)

10 20 30 3 20
Number 1 add Number 2 add Number 3 divide Count arrow_forward Average

Average = (Sum of all numbers) ÷ (Count of numbers)


Simple Interest Calculator

monetization_on A simple program to calculate simple interest

# Program to calculate simple interest


principal = 1000
rate = 5
time = 2
interest = (principal * rate * time) / 100
print("The simple interest is", interest)

Simple Interest Formula


SI = (P × R × T) / 100

Principal Rate Time Interest


1000 5% 2 years 100
Checking if a Year is a Leap Year

event A simple program to determine if a year is a leap year

# Program to check if a year is a leap year


year = 2024
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")

info Leap Year Rules code Logic Explanation


Example
A year is a leap year if: The code checks two conditions:
2024
Divisible by 4 1. (year % 4 == 0 and year % 100 != 0)
2024 ÷ 4 = 506 (no remainder)
But NOT divisible by 100 Divisible by 4 but not by 100
Unless it's also divisible by 400 2024 ÷ 100 = 20.24 (remainder)
2. (year % 400 == 0)
Result: Leap Year
Divisible by 400 (like year 2000)

You might also like