[go: up one dir, main page]

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

Flow Control Programs

programs of flow of control

Uploaded by

preetyadav340
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)
10 views3 pages

Flow Control Programs

programs of flow of control

Uploaded by

preetyadav340
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
You are on page 1/ 3

1.

Check Leap Year


year = 2024
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print("Leap Year")
else:
print("Not a Leap Year")
Output:
Leap Year

2. Find Maximum of List (without max())


nums = [5, 9, 1, 12, 7]
largest = nums[0]
for n in nums:
if n > largest:
largest = n
print("Largest:", largest)
Output:
Largest: 12

3. Count Even and Odd Numbers


nums = [10, 21, 4, 45, 66, 93]
even, odd = 0, 0
for n in nums:
if n % 2 == 0:
even += 1
else:
odd += 1
print("Even:", even, "Odd:", odd)
Output:
Even: 3 Odd: 3

4. Print Prime Numbers in Range


for n in range(10, 21):
for i in range(2, n):
if n % i == 0:
break
else:
print(n, end=" ")
Output:
11 13 17 19

5. Perfect Number
n = 28
s = sum(i for i in range(1, n) if n % i == 0)
print("Perfect" if s == n else "Not Perfect")
Output:
Perfect

6. Sum of Digits
n = 12345
s = 0
while n > 0:
s += n % 10
n //= 10
print("Sum of digits:", s)
Output:
Sum of digits: 15
7. Product of Digits
n = 234
prod = 1
while n > 0:
prod *= n % 10
n //= 10
print("Product:", prod)
Output:
Product: 24

8. Armstrong Numbers (100–999)


for n in range(100, 130):
s = sum(int(d)**3 for d in str(n))
if s == n:
print(n, end=" ")
Output:
153

9. Fibonacci up to 50
a, b = 0, 1
while a <= 50:
print(a, end=" ")
a, b = b, a+b
Output:
0 1 1 2 3 5 8 13 21 34

10. Reverse List


nums = [1, 2, 3, 4, 5]
rev = []
for i in range(len(nums)-1, -1, -1):
rev.append(nums[i])
print(rev)
Output:
[5, 4, 3, 2, 1]

11. Multiplication Tables 1–2


for n in range(1, 3):
for i in range(1, 6):
print(f"{n} x {i} = {n*i}")
print()
Output:
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10

12. Sum of Squares


n = 5
s = 0
for i in range(1, n+1):
s += i**2
print("Sum of squares:", s)
Output:
Sum of squares: 55

13. Palindrome Number Check


n = 1221
temp, rev = n, 0
while temp > 0:
rev = rev*10 + temp%10
temp //= 10
print("Palindrome" if rev == n else "Not Palindrome")
Output:
Palindrome

14. LCM of Two Numbers


a, b = 15, 20
mx = max(a, b)
while True:
if mx % a == 0 and mx % b == 0:
print("LCM:", mx)
break
mx += 1
Output:
LCM: 60

15. Pyramid Pattern


n = 5
for i in range(1, n+1):
print(" "*(n-i) + "*"*(2*i-1))
Output:
*
***
*****
*******
*********

You might also like