0% found this document useful (0 votes)
20 views2 pages

Number-Based While Loop Programs

Uploaded by

dishan.ghosh04
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)
20 views2 pages

Number-Based While Loop Programs

Uploaded by

dishan.ghosh04
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

Number-Based While Loop Programs (Digit Extraction)

1. Count the number of digits


n = 12345
count = 0
while n > 0:
count += 1
n //= 10
print("Count of digits:", count)

2. Sum of digits
n = 1234
s = 0
while n > 0:
s += n % 10
n //= 10
print("Sum of digits:", s)

3. Product of digits
n = 234
prod = 1
while n > 0:
prod *= n % 10
n //= 10
print("Product of digits:", prod)

4. Reverse the number


n = 1234
rev = 0
while n > 0:
rev = rev * 10 + n % 10
n //= 10
print("Reversed number:", rev)

5. Check if number is palindrome


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

6. Check if number is Armstrong


n = 153
temp = n
s = 0
while n > 0:
d = n % 10
s += d ** 3
Number-Based While Loop Programs (Digit Extraction)

n //= 10
print("Armstrong" if temp == s else "Not Armstrong")

7. Check if number is Strong


import math
n = 145
temp = n
s = 0
while n > 0:
d = n % 10
s += [Link](d)
n //= 10
print("Strong number" if temp == s else "Not a Strong number")

8. Count even and odd digits


n = 123456
even = odd = 0
while n > 0:
d = n % 10
if d % 2 == 0:
even += 1
else:
odd += 1
n //= 10
print("Even:", even, "Odd:", odd)

9. Find largest digit


n = 49275
max_d = 0
while n > 0:
d = n % 10
if d > max_d:
max_d = d
n //= 10
print("Largest digit:", max_d)

10. Find smallest digit


n = 49275
min_d = 9
while n > 0:
d = n % 10
if d < min_d:
min_d = d
n //= 10
print("Smallest digit:", min_d)

You might also like