0% found this document useful (0 votes)
3 views4 pages

Python Control Flow and Loop Examples

The document contains various Python programs demonstrating control flow and loops, including examples for checking even/odd numbers, finding the largest and smallest among three numbers, calculating factorials, generating Fibonacci sequences, and summing prime numbers. It also includes functionalities like checking for prime numbers, palindrome checking, Armstrong numbers, and a simple calculator. Each program is presented with code snippets and user input prompts.

Uploaded by

sindhuja
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)
3 views4 pages

Python Control Flow and Loop Examples

The document contains various Python programs demonstrating control flow and loops, including examples for checking even/odd numbers, finding the largest and smallest among three numbers, calculating factorials, generating Fibonacci sequences, and summing prime numbers. It also includes functionalities like checking for prime numbers, palindrome checking, Armstrong numbers, and a simple calculator. Each program is presented with code snippets and user input prompts.

Uploaded by

sindhuja
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

Python Programs: Control Flow and Loops

Even or Odd Checker


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

Largest Among Three Numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b and a >= c:


print("Largest:", a)
elif b >= a and b >= c:
print("Largest:", b)
else:
print("Largest:", c)

Factorial Using While Loop


num = int(input("Enter a number: "))
fact = 1
while num > 0:
fact *= num
num -= 1
print("Factorial:", fact)

Fibonacci Sequence Using For Loop


n = int(input("Enter number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b

Sum of Prime Numbers in Range


start = int(input("Enter start: "))
end = int(input("Enter end: "))
total = 0

for num in range(start, end + 1):


if num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
total += num

print("Sum of primes:", total)

Number Divisible by 2 and 3


num = int(input("Enter a number: "))
if num % 2 == 0 and num % 3 == 0:
print("Divisible by 2 and 3")
else:
print("Not divisible by both")

Switch-Case Like Behavior (Day of Week)


day = int(input("Enter day number (1-7): "))
if day == 1:
print("Monday")
elif day == 2:
print("Tuesday")
elif day == 3:
print("Wednesday")
elif day == 4:
print("Thursday")
elif day == 5:
print("Friday")
elif day == 6:
print("Saturday")
elif day == 7:
print("Sunday")
else:
print("Invalid")

Skip Numbers Divisible by 3


for i in range(1, 11):
if i % 3 == 0:
continue
print(i)

Break on Number 5
for i in range(1, 11):
if i == 5:
break
print(i)

Smallest Among Three Numbers


a = int(input("Enter first: "))
b = int(input("Enter second: "))
c = int(input("Enter third: "))

if a <= b and a <= c:


print("Smallest:", a)
elif b <= a and b <= c:
print("Smallest:", b)
else:
print("Smallest:", c)

Prime Number Checker


num = int(input("Enter number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")

Multiplication Table
for i in range(1, 11):
for j in range(1, 11):
print(i * j, end=" ")
print()

Palindrome Checker
s = input("Enter string: ")
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")

Armstrong Number
num = int(input("Enter number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if sum == num:
print("Armstrong")
else:
print("Not Armstrong")

Simple Calculator
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Enter operator (+, -, *, /): ")

if op == '+':
print("Result:", a + b)
elif op == '-':
print("Result:", a - b)
elif op == '*':
print("Result:", a * b)
elif op == '/':
if b != 0:
print("Result:", a / b)
else:
print("Cannot divide by 0")
else:
print("Invalid operator")

You might also like