DEV Community

Cover image for Python tasks - 6
G Gokul
G Gokul

Posted on

Python tasks - 6

Task - find count of a digits

Program:

no = int(input ("enter a number:"))
count = 0
while(no>0):
    no%10
    no = no // 10
    count += 1
print(f'count of digit is {count}')
Enter fullscreen mode Exit fullscreen mode

Output:
enter a number:2345
count of digit is 4

Task - find sum of digits

Program:

no = int(input ("enter a number:"))
count = 0
total = 0
while(no>0):
    total = total + no%10
    no = no // 10
    count += 1
print(f'count of digit is {count}')
print(f'sum of digit is {total}')
Enter fullscreen mode Exit fullscreen mode

Output:
enter a number:1234
count of digit is 4
sum of digit is 10

Task - reverse a number

Program:

no = int(input ("enter a number:"))
reverse = 0
while(no>0):
reverse= (reverse*10) + no%10
no = no // 10
print(f'reverse is {reverse}')
Enter fullscreen mode Exit fullscreen mode

Output:
enter a number:4567
reverse is 7654

Task - find given number is palindrome or not

Program:

no = int(input ("enter a number:"))
reverse = 0
copy = no
while(no>0):
    reverse = (reverse*10) + no%10
    no = no // 10
if copy == reverse:
    print("palindrome")
else:
    print("not palindrome")
Enter fullscreen mode Exit fullscreen mode

Output:
enter a number:121
palindrome

Top comments (0)