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

5 Programs Using If Statement

Uploaded by

abhiudayas.epost
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)
25 views

5 Programs Using If Statement

Uploaded by

abhiudayas.epost
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/ 4

5 Programs using if Statement:

1) Checking if a number is positive or negative:

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


if number >= 0:
print("The number is positive.")
else:
print("The number is negative.")

2) Checking if ‘a’ is in the word:

text = input("Enter a string: ")


if 'a' in text:
print("The string contains the letter 'a'.")
else:
print("The string does not contain the letter 'a'.")

3) Check if a person can drive (age 18 or older):

age = int(input("Enter your age: "))


if age >= 18:
print("You can legally drive.")
else:
print("You cannot legally drive.")

4) Check if a number is a multiple of 4:

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


if number % 4 == 0:
print("The number is a multiple of 4.")
else:
print("The number is not a multiple of 4.")

5) Check if a temperature is hot (above 30°C):

temperature = float(input("Enter the temperature in °C: "))


if temperature > 30:
print("It's hot!")
else:
print("It's not hot.")

5 Programs using while Loop:

1. Print numbers from 1 to 5:


i=1
while i <= 5:
print(i)
i += 1

2. Print only even numbers from 1 to 10:


i=1
while i <= 10:
if i % 2 == 0:
print(i)
i += 1

3. Print the sum of numbers from 1 to 100:


i=1
total = 0
while i <= 100:
total += i
i += 1
print(f"Sum of numbers from 1 to 100 is: {total}")

4. Repeat a string 5 times:


i=1
text = input("Enter a string: ")
while i <= 5:
print(text)
i += 1

5. Find the factorial of a number:


num = int(input("Enter a number: "))
factorial = 1
while num > 0:
factorial *= num
num -= 1
print(f"The factorial is: {factorial}")
5 Programs using for Loop:
1. Print each letter of a string:
text = input("Enter a string: ")
for char in text:
print(char)

2. Print numbers from 1 to 10:


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

3. Print all elements of a list:


my_list = [2, 4, 6, 8, 10]
for element in my_list:
print(element)

4. Calculate the sum of a list of numbers:


numbers = [1, 2, 3, 4, 5]
total = 0
for num in numbers:
total += num
print(f"The sum is: {total}")

5. Print the multiplication table of a number:


number = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{number} x {i} = {number * i}")
Another 5 programs:

1. Check if a number is 10:

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

if number == 10:
print("The number is 10!")
else:
print("The number is not 10.")

2. Print "Hello" 5 times:

i=0

while i < 5:
print("Hello")
i += 1

3. Print each letter in "hi":

for letter in "hi":


print(letter)

4. Tell if a string is empty:

text = input("Enter something: ")

if text == "":
print("You entered nothing!")
else:
print("You entered something!")

5. Add 1 to a number:

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


print("Your number plus 1 is:", number + 1)
By Abhinam Dhungana

You might also like