1.Count of Digits
counts the number of digits in a number using a while loop.
no = 7654
count = 0
while no>0:
no = no//10
count+=1
else:
print ("count of digits : " , count) //count of digits : 4
My attempt
2.Sum of Digits
sum all the given digits
no = 7654
count = 0
sum_of_digits = 0
while no>0:
sum_of_digits = sum_of_digits + no%10
no = no//10
count += 1
else:
print ("count of digits : " , count) //4
print ("sum_of_digits: " , sum_of_digits) //22
3.Reverse the number
no = 12321
count = 0
reverse = 0
while no>0:
reverse = (reverse*10) + no%10
no = no//10
count += 1
else:
print ("count of digits : " , count) //5
print ("reverse the number : " , reverse) //12321
In python we use else for while statement also.
4.Palindrome
A palindrome number reads the same forward and backward.
no = 12321
no2 = no
count = 0
reverse = 0
while no>0:
reverse = (reverse*10) + no%10
no = no//10
count += 1
else:
print ("count of digits : " , count) //4
print ("reverse the number : " , reverse) //12321
if(no2 == reverse):
print("palindrome")
else:
print("not palindrome")
Through this program It feels like One concept lead to another.
Every small logic we learn becomes a building block for solving bigger problems.



Top comments (0)