0% found this document useful (0 votes)
21 views6 pages

Python Logic Programs with Outputs

The document contains a series of Python programs that demonstrate various logical thinking and problem-solving techniques. Each program includes a brief description, code, and sample output for tasks such as calculating special sums, checking magic numbers, counting palindromic numbers, and converting numbers to words. The programs cover a wide range of topics, showcasing different programming concepts and algorithms.

Uploaded by

amitsweden17
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)
21 views6 pages

Python Logic Programs with Outputs

The document contains a series of Python programs that demonstrate various logical thinking and problem-solving techniques. Each program includes a brief description, code, and sample output for tasks such as calculating special sums, checking magic numbers, counting palindromic numbers, and converting numbers to words. The programs cover a wide range of topics, showcasing different programming concepts and algorithms.

Uploaded by

amitsweden17
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 Logical Thinking Programs (Code + Output)

1. Special Sum Pattern


n = int(input("Enter number of terms: "))
num = 0
total = 0
for i in range(n):
num = num * 10 + 1
total += num
print("Sum:", total)
Output:
Enter number of terms: 4
Sum: 1234

2. Alternating Series Sum


n = int(input("Enter number of terms: "))
total = 0
for i in range(1, n + 1):
if i % 2 == 0:
total -= i
else:
total += i
print("Result:", total)
Output:
Enter number of terms: 6
Result: 3

3. Digit Frequency Counter


num = input("Enter a number: ")
for i in range(10):
count = [Link](str(i))
if count > 0:
print(f"{i} → {count} times")
Output:
Enter a number: 1122235
1 → 2 times
2 → 3 times
3 → 1 time
5 → 1 time

4. Magic Number Checker


num = int(input("Enter number: "))
n = num
while n > 9:
s = 0
while n > 0:
s += n % 10
n //= 10
n = s
if n == 1:
print(num, "is a Magic Number")
else:
print(num, "is not a Magic Number")
Output:
Enter number: 19
19 is a Magic Number

5. Perfect Square Pattern


n = int(input("Enter number: "))
for i in range(n, 0, -1):
for j in range(i, 0, -1):
print(j*j, end=" ")
print()
Output:
Enter number: 4
16 9 4 1
9 4 1
4 1
1

6. Rearrange Digits to Form Largest Number


num = input("Enter number: ")
digits = sorted(num, reverse=True)
largest = "".join(digits)
print("Largest possible number:", largest)
Output:
Enter number: 43921
Largest possible number: 94321

7. Find Missing Number in a Sequence


lst = list(map(int, input("Enter numbers separated by space: ").split()))
for i in range(min(lst), max(lst) + 1):
if i not in lst:
print("Missing number:", i)
break
Output:
Enter numbers separated by space: 1 2 3 5 6
Missing number: 4

8. Number Mirror Pattern


n = int(input("Enter rows: "))
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end="")
print(" " * (2 * (n - i)), end="")
for j in range(i, 0, -1):
print(j, end="")
print()
Output:
Enter rows: 4
1 1
12 21
123 321
12344321

9. Harshad (Niven) Number


num = int(input("Enter number: "))
s = sum(int(d) for d in str(num))
if num % s == 0:
print("Harshad Number")
else:
print("Not a Harshad Number")
Output:
Enter number: 18
Harshad Number

10. Sum of First and Last Digit Until 0


while True:
num = int(input("Enter number (0 to stop): "))
if num == 0:
break
s = str(num)
print("Sum of first and last digit:", int(s[0]) + int(s[-1]))
Output:
Enter number: 1234
Sum of first and last digit: 5

11. Print Numbers Without Using + or -


def print_nums(n):
if n == 0:
return
print_nums(n - 1)
print(n)

n = int(input("Enter limit: "))


print_nums(n)
Output:
Enter limit: 5
1
2
3
4
5

12. Binary Pattern Challenge


n = int(input("Enter number of rows: "))
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j % 2, end="")
print()
Output:
Enter number of rows: 5
1
10
101
1010
10101

13. Digit Product Puzzle


num = input("Enter number: ")
product = 1
for d in num:
if d != '0':
product *= int(d)
print("Product of non-zero digits:", product)
Output:
Enter number: 10523
Product of non-zero digits: 30

14. Count Palindromic Numbers (1–N)


n = int(input("Enter limit: "))
count = 0
for i in range(1, n + 1):
if str(i) == str(i)[::-1]:
count += 1
print("Total palindrome numbers:", count)
Output:
Enter limit: 20
Total palindrome numbers: 11

15. Strong Numbers


import math
for i in range(1, 501):
s = sum([Link](int(d)) for d in str(i))
if s == i:
print(i, end=" ")
Output:
1 2 145

16. Lucky Number (Elimination Game)


n = int(input("Enter limit: "))
numbers = list(range(1, n + 1))
i = 2
while i <= len(numbers):
numbers = [num for idx, num in enumerate(numbers, start=1) if idx % i != 0]
i += 1
print("Lucky number:", numbers[0])
Output:
Enter limit: 10
Lucky number: 7

17. ATM Note Counter


amt = int(input("Enter amount: "))
notes = [2000, 500, 100, 50, 10]
for note in notes:
count = amt // note
if count:
print(f"{note} x {count}")
amt %= note
Output:
Enter amount: 2860
2000 x 1
500 x 1
100 x 3
50 x 1
10 x 1

18. Convert Number to Words


ones = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
tens = ["", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninet

num = int(input("Enter number (1–9999): "))

def convert(n):
if n < 10:
return ones[n]
elif n < 100:
return tens[n // 10] + " " + ones[n % 10]
elif n < 1000:
return ones[n // 100] + " Hundred " + convert(n % 100)
else:
return ones[n // 1000] + " Thousand " + convert(n % 1000)

print(convert(num))
Output:
Enter number: 4321
Four Thousand Three Hundred Twenty-One

19. Perfect Number Between 1–1000


for i in range(1, 1001):
s = 0
for j in range(1, i):
if i % j == 0:
s += j
if s == i:
print(i, end=" ")
Output:
6 28 496

20. Digital Root


num = int(input("Enter number: "))
while num > 9:
num = sum(int(d) for d in str(num))
print("Digital Root:", num)
Output:
Enter number: 9875
Digital Root: 2

You might also like