Computer python project
Computer python project
Program
Question: Write a program to check if a given number is
even or odd.
Code:
Python:
num = int(input("Enter a number: "))
if num % 2 == 0:
print(f"{num} is an even number.")
else:
print(f"{num} is an odd number.")
Output Example:
Enter a number: 7
7 is an odd number.
Program 2: If-else Statement-Based
Program
Question: Write a program to categorize a person based on
their age.
Code:
Python:
age = int(input("Enter your age: "))
if age < 13:
print("You are a Child.")
elif age < 20:
print("You are a Teenager.")
elif age < 60:
print("You are an Adult.")
else:
print("You are a Senior Citizen.")
Output Example:
mathematica
CopyEdit
Enter your age: 25
You are an Adult.
Program 3: For Loop / While Loop-Based
Program
Question: Write a program to print the multiplication table of a
given number using a for loop.
Code:
Python:
num = int(input("Enter a number: "))
Output Example:
Enter a number: 5
Multiplication table for 5:
5x1=5
5 x 2 = 10
...
5 x 10 = 50
Program 4: Series Program
Question: Write a program to calculate the sum of the first N
natural numbers.
Code:
python
n = int(input("Enter the value of N: "))
# Using the formula for the sum of first N natural numbers
sum_n = n * (n + 1) // 2
print(f"The sum of the first {n} natural numbers is {sum_n}.")
Output Example:
Enter the value of N: 10
The sum of the first 10 natural numbers is 55.