本文概述
- C++
- Java
- Python3
- C#
- PHP
例子:
Input: N = 2002
Output: trueInput: N = 1234
Output: false
文章图片
方法:
解决这个问题的一个简单方法是,首先将n的数字进行逆序,然后将n的逆序与n进行比较。如果两者相同,则返回true,否则返回false。
下面是上述方法的实现:
C++
//C program to check whether a number
//is Palindrome or not.#include <
stdio.h>
/* Iterative function to reverse digits of num*/
int reverseDigits( int num)
{
int rev_num = 0;
while (num>
0) {
rev_num = rev_num * 10 + num % 10;
num = num /10;
}
return rev_num;
}/* Function to check if n is Palindrome*/
int isPalindrome( int n)
{//get the reverse of n
int rev_n = reverseDigits(n);
//Check if rev_n and n are same or not.
if (rev_n == n)
return 1;
else
return 0;
}/*Driver program to test reversDigits*/
int main()
{
int n = 4562;
printf ( "Is %d a Palindrome number? ->
%s\n" , n, isPalindrome(n) == 1 ? "true" : "false" );
n = 2002;
printf ( "Is %d a Palindrome number? ->
%s\n" , n, isPalindrome(n) == 1 ? "true" : "false" );
return 0;
}
Java
//Java program to check whether a number
//is Palindrome or not.class GFG
{
/* Iterative function to reverse digits of num*/
static int reverseDigits( int num)
{
int rev_num = 0 ;
while (num>
0 ) {
rev_num = rev_num * 10 + num % 10 ;
num = num /10 ;
}
return rev_num;
}/* Function to check if n is Palindrome*/
static int isPalindrome( int n)
{//get the reverse of n
int rev_n = reverseDigits(n);
//Check if rev_n and n are same or not.
if (rev_n == n)
return 1 ;
else
return 0 ;
}/*Driver program to test reversDigits*/
public static voidmain(String []args)
{
int n = 4562 ;
System.out.println( "Is" + n + "a Palindrome number? ->
" +
(isPalindrome(n) == 1 ? "true" : "false" ));
n = 2002 ;
System.out.println( "Is" + n + "a Palindrome number? ->
" +
(isPalindrome(n) == 1 ? "true" : "false" ));
}}//This code is contributed
//by Hritik Raj ( ihritik )
Python3
# Python3 program to check whether a
# number is Palindrome or not. # Iterative function to reverse
# digits of num
def reverseDigits(num) : rev_num = 0 ;
while (num>
0 ) :
rev_num = rev_num * 10 + num % 10
num = num //10return rev_num # Function to check if n is Palindrome
def isPalindrome(n) :# get the reverse of n
rev_n = reverseDigits(n);
# Check if rev_n and n are same or not.
if (rev_n = = n) :
return 1
else :
return 0# Driver Code
if __name__ = = "__main__" :n = 4562if isPalindrome(n) = = 1 :
print ( "Is" , n, "a Palindrome number? ->
" , True )else :
print ( "Is" , n, "a Palindrome number? ->
" , False )n = 2002if isPalindrome(n) = = 1 :
print ( "Is" , n, "a Palindrome number? ->
" , True )else :
print ( "Is" , n, "a Palindrome number? ->
" , False )# This code is contributed by Ryuga
C#
//C# program to check whether a number
//is Palindrome or not.using System;
class GFG
{
/* Iterative function to reverse digits of num*/
static int reverseDigits( int num)
{
int rev_num = 0;
while (num>
0) {
rev_num = rev_num * 10 + num % 10;
num = num /10;
}
return rev_num;
}/* Function to check if n is Palindrome*/
static int isPalindrome( int n)
{//get the reverse of n
int rev_n = reverseDigits(n);
//Check if rev_n and n are same or not.
if (rev_n == n)
return 1;
else
return 0;
}/*Driver program to test reversDigits*/
public static voidMain()
{
int n = 4562;
Console.WriteLine( "Is" + n + "a Palindrome number? ->
" +
(isPalindrome(n) == 1 ? "true" : "false" ));
n = 2002;
Console.WriteLine( "Is" + n + "a Palindrome number? ->
" +
(isPalindrome(n) == 1 ? "true" : "false" ));
}}//This code is contributed
//by Hritik Raj ( ihritik )
PHP
<
?php
//PHP program to check whether a number
//is Palindrome or not.//Iterative function to reverse
//digits of num
function reverseDigits( $num )
{
$rev_num = 0;
while ( $num>
0)
{
$rev_num = $rev_num * 10 +
$num % 10;
$num = $num /10;
}
return $rev_num ;
}//Function to check if n is Palindrome
function isPalindrome( $n )
{//get the reverse of n
$rev_n = reverseDigits( $n );
//Check if rev_n and n are same or not.
if ( $rev_n == $n )
return 1;
else
return 0;
}//Driver Code
$n = 4562;
echo "Is " , $n , " a Palindrome number? ->
" ;
if (isPalindrome( $n ) == 1)
echo "true" ;
else
echo "false" ;
echo "\n" ;
$n = 2002;
echo "Is " , $n , " a Palindrome number? ->
" ;
if (isPalindrome(! $n ))
echo "true" ;
else
echo "false" ;
//This code is contributed by jit_t
?>
【算法题(检查数字是否为回文)】输出如下:
Is 4562 a Palindrome number? ->
false
Is 2002 a Palindrome number? ->
true
推荐阅读
- 概率统计|辛普森悖论(加州大学伯克利分校的诉讼)
- Google软件工程实习生,2019年秋季–北美
- 德里面试经验– 1年经验
- 对字母数字字符串进行排序,以使字母和数字的位置保持不变
- 缓存中的透写和回写是什么(详细介绍)
- Win8系统没有组策略的原因区分与处理办法
- Win8安装mssql2005提示打开服务失败的处理办法
- Win8.1系统找不到连接到Microsoft帐户怎样办?
- 如何应对Win8.1应用商店无法完成购买出错0x80096004的问题