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

Control Structures 20 Programs

The document provides a series of Python code snippets demonstrating basic programming concepts such as checking if a number is positive or negative, determining even or odd numbers, and finding the largest of two or three numbers. It also includes examples of grade calculation, leap year checking, and simple calculator operations, as well as loops for printing numbers, calculating sums, and generating patterns. Additionally, it covers advanced topics like skipping odd numbers and reversing a number using loops.

Uploaded by

gg9244260
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 views3 pages

Control Structures 20 Programs

The document provides a series of Python code snippets demonstrating basic programming concepts such as checking if a number is positive or negative, determining even or odd numbers, and finding the largest of two or three numbers. It also includes examples of grade calculation, leap year checking, and simple calculator operations, as well as loops for printing numbers, calculating sums, and generating patterns. Additionally, it covers advanced topics like skipping odd numbers and reversing a number using loops.

Uploaded by

gg9244260
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

1.

Check if Number is Positive or Negative


num = int(input("Enter a number: "))
if num > 0:
print("Positive")
else:
print("Negative or Zero")

2. Check Even or Odd


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

3. Find Largest of Two Numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a > b:
print(a, "is greater")
else:
print(b, "is greater")

4. Find Largest of Three Numbers (Nested if)


a = 10
b = 25
c = 15
if a > b:
if a > c:
print("A is largest")
else:
print("C is largest")
else:
if b > c:
print("B is largest")
else:
print("C is largest")

5. Grade Calculation (if-elif ladder)


marks = int(input("Enter marks: "))
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")

6. Check Leap Year


year = int(input("Enter year: "))
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print("Leap Year")
else:
print("Not Leap Year")

7. Check Character is Vowel or Consonant


ch = input("Enter a letter: ").lower()
if ch in ['a', 'e', 'i', 'o', 'u']:
print("Vowel")
else:
print("Consonant")

8. Simple Calculator (if-elif)


a = int(input("Enter first number: "))
b = int(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 == '/':
print("Result:", a / b)
else:
print("Invalid Operator")

9. Check for Divisibility


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

10. Print 1 to 10 using While Loop


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

11. Sum of First 10 Natural Numbers (While Loop)


i = 1
total = 0
while i <= 10:
total += i
i += 1
print("Sum =", total)

12. Factorial Using While Loop


num = int(input("Enter number: "))
fact = 1
i = 1
while i <= num:
fact *= i
i += 1
print("Factorial:", fact)

13. Print Multiplication Table (For Loop)


n = int(input("Enter a number: "))
for i in range(1, 11):
print(n, "x", i, "=", n * i)

14. Print Even Numbers from 1 to 20


for i in range(2, 21, 2):
print(i)

15. Find Sum of List Elements (For Loop)


nums = [3, 5, 7, 9]
total = 0
for n in nums:
total += n
print("Sum =", total)

16. Nested Loops – Pattern 1 (Triangle of Stars)


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

17. Nested Loops – Multiplication Table 1 to 3


for i in range(1, 4):
for j in range(1, 6):
print(i, "x", j, "=", i * j)
print("-----")

18. Skip Odd Numbers using continue


for i in range(1, 10):
if i % 2 != 0:
continue
print(i)

19. Demonstrate pass Statement


for i in range(5):
if i == 3:
pass # placeholder – does nothing
else:
print("Value:", i)

20. Reverse Number using While Loop


num = int(input("Enter a number: "))
rev = 0
while num > 0:
digit = num % 10
rev = rev * 10 + digit
num //= 10
print("Reversed number:", rev)

You might also like