Python continue statement and while loop
Check if a given number is a palindrome.
n=int(input("Enter a number"))
reverse=0
original_num=n
while n>0:
last_digit=n%10
reverse=reverse*10+last_digit
n=n//10
if reverse==original_num:
print("Number is a palindrome")
else:
print("Number is not a palindrome")
Output
Find the sum of digits of the number. Get the number from the user.
n=int(input("Enter a number"))
sum=0
while n>0:
last_digit=n%10
sum=sum+last_digit
n=n//10
print("The sum of the digits is ",sum)
Output
Check if a given number is armstrong number
n=int(input("Enter a number"))
sum=0
original_num=n
num_len=len(str(n))
while n>0:
last_digit=n%10
sum=sum+last_digit**num_len
n=n//10
if sum==original_num:
print("you have an armstrong number")
else:
print("Its not a armstrong number")
Output
Convert a given binary number to decimal
n=int(input("enter the binary number "))
i=0
dec=0
while n>0:
last_digit=n%10
dec = dec+last_digit*(2**i)
i=i+1
n=n//10
print("The decimal number is", dec)
Output
Check if a given number is an Adams number – The reverse of the square of a number is the square of reverse of that number .If the input is 12 then square of 12 is 144. The reverse of 144 is 441 and that is 21 square.
n=int(input("enter the number "))
input_square=n**2
back_up=input_square
reverse_input_square=0
last_digit=0
while input_square>0:
last_digit=input_square%10
reverse_input_square=reverse_input_square*10+last_digit
input_square=input_square//10
reverse_num=0
while n>0:
last_digit=n%10
reverse_num=reverse_num*10+last_digit
n=n//10
if reverse_input_square==(reverse_num**2):
print("Its an adams number")
else:
print("Its not an adams number")
Output
Write a program that reads an integer and determines and prints how many digits
in the integer are 7’s.
n=int(input("enter the count "))
last_digit=0
count=0
while n>0:
last_digit=n%10
if last_digit==7:
count=count+1
n=n//10
print("The count of 7s is ",count)
Output
Write an program to find the sum of digits of any integer entered by the user .For example, if the user enters 123 then the result 6(sum of 1+2+3) should be displayed to the user
n=int(input("enter the count "))
last_digit=0
sum=0
while n>0:
last_digit=n%10
sum=sum+last_digit
n=n//10
print("The sum is ",sum)
Output