Computer Science, asked by dynamosgaming, 1 year ago

Retain driven program to accept a number from the user and check whether it is a palindrome or a perfect number


LuckyYadav2578: in which language

Answers

Answered by LuckyYadav2578
0

In Python   For palindrome  n= int ( input ("Enter any number ")) c=n d=0 while (n>0) : :\ a=n%10 _______ n=n//10 _______ d=d*10+a if d==n: _______ print (" it is a palindrome ") else:  _______ print ("it is not a palindrome ")  Dry Run  suppose we are taking a number 123 (as we know it is not a palindrome ) n = 12 c = 12 ....... (as c=n) inside the loop first time 12 > 0  ........... (n> 0) while statement  a = 2 .............. ( a =n%10 => a=12%10 ) n = 1 .............. ( n = n//10 => n= 12//10 ) d = 2 .............. ( d =d*10+a => d= 0*10+2) inside the loop second time 1>0 ............... (n> 0 => n=1) while statement  a = 1 .............. ( a =n%10 => a=1%10 ) n = 0 .............. ( n = n//10 => n= 1//10) d = 21 .............. ( d =d*10+a => d= 2*10+1) inside the loop third time 0>0 ...............( n>0 condition false ) out from the loop d==n .............( condition false because n=12 and d=21 ) it will print the else condition it is not a palindrome.  note : I am using this --> ( ______ ) for inedntation. Imagine it invisible.


LuckyYadav2578: Dear i m sorry but it is a dry run of 12
Answered by 2578lucky
2

In Python  

For palindrome  

n= int ( input ("Enter any number "))

c=n

d=0

while (n>0) :

_______ a=n%10

_______ n=n//10

_______ d=d*10+a

if d==n:

_______ print (" it is a palindrome ")

else:  

_______ print ("it is not a palindrome ")

Dry Run  

  • suppose we are taking a number 12 (as we know it is not a palindrome )
  • n = 12
  • c = 12 ....... (as c=n)
  • inside the loop first time
  • 12 > 0  ........... (n> 0) while statement  a = 2 .............. ( a =n%10 => a=12%10 )
  • n = 1 .............. ( n = n//10 => n= 12//10 )
  • d = 2 .............. ( d =d*10+a => d= 0*10+2)
  • inside the loop second time
  • 1>0 ............... (n> 0 => n=1) while statement  a = 1 .............. ( a =n%10 => a=1%10 )
  • n = 0 .............. ( n = n//10 => n= 1//10)
  • d = 21 .............. ( d =d*10+a => d= 2*10+1)
  • inside the loop third time
  • 0>0 ...............( n>0 condition false )
  • out from the loop
  • d==n .............( condition false because n=12 and d=21 )
  • it will print the else condition
  • it is not a palindrome

 

note : I am using this --> ( ______ ) for inedntation. Imagine it invisible.

Similar questions