简体   繁体   中英

Method to check a prime palindrome number

How to check if a number is palindrome number or not and if it is, follow with checking if it is a prime number too.

How to check if a number is palindrome number or not and if it is, follow with checking if it is a prime number too.

For palindrome number, you can go with this algorithm:

  1. Store user input in a variable ( num )
  2. Store the value of the integer in another temporary variable ( temp )
  3. reverse the number
  4. compare the temp with num
  5. if they are same, the number is palindrome
  6. Else it is not a palindrome number

For Prime number:

  1. store user input in a variable ( temp )
  2. divide the number from 2 to temp-1 using For loop
  3. If we find a factor in that range, the number is not prime
  4. other wise it is a prime
   temp = num
   def is_palindrome():
       global num
       global temp
       rev = 0
       while (num > 0):
           dig = num % 10
           rev = rev * 10 + dig
           num = num // 10
       if (temp == rev):
           print("The number is palindrome!")
       else:
           print("This is Not a palindrome!")
   
   
   def prime_checker():
       global temp
       is_prime = True
       for N in range(2, temp):
           if (temp % N) == 0:
               is_prime = False
               break
       if is_prime:
           print("It's a prime number.")
       else:
           print("It's not a prime number.")
   
   is_palindrome()
   prime_checker()```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM