给定一个数字,任务是编写一个递归函数,检查给定的数字是否为回文。 例如:
null
Input : 121Output : yesInput : 532Output : no
编写函数的方法是递归调用函数,直到数字从后面完全遍历为止。根据在中获得的公式,使用temp变量存储数字的倒数 这 邮递在参数中传递temp变量,一旦达到n==0的基本情况,返回存储数字倒数的temp。 以下是上述方法的实施情况:
C++
// Recursive C++ program to check if the // number is palindrome or not #include <bits/stdc++.h> using namespace std; // recursive function that returns the reverse of digits int rev( int n, int temp) { // base case if (n == 0) return temp; // stores the reverse of a number temp = (temp * 10) + (n % 10); return rev(n / 10, temp); } // Driver Code int main() { int n = 121; int temp = rev(n, 0); if (temp == n) cout << "yes" << endl; else cout << "no" << endl; return 0; } |
JAVA
// Recursive Java program to // check if the number is // palindrome or not import java.io.*; class GFG { // recursive function that // returns the reverse of digits static int rev( int n, int temp) { // base case if (n == 0 ) return temp; // stores the reverse // of a number temp = (temp * 10 ) + (n % 10 ); return rev(n / 10 , temp); } // Driver Code public static void main (String[] args) { int n = 121 ; int temp = rev(n, 0 ); if (temp == n) System.out.println( "yes" ); else System.out.println( "no" ); } } // This code is contributed by anuj_67. |
Python3
# Recursive Python3 program to check # if the number is palindrome or not # Recursive function that returns # the reverse of digits def rev(n, temp): # base case if (n = = 0 ): return temp; # stores the reverse of a number temp = (temp * 10 ) + (n % 10 ); return rev(n / / 10 , temp); # Driver Code n = 121 ; temp = rev(n, 0 ); if (temp = = n): print ( "yes" ) else : print ( "no" ) # This code is contributed # by mits |
C#
// Recursive C# program to // check if the number is // palindrome or not using System; class GFG { // recursive function // that returns the // reverse of digits static int rev( int n, int temp) { // base case if (n == 0) return temp; // stores the reverse // of a number temp = (temp * 10) + (n % 10); return rev(n / 10, temp); } // Driver Code public static void Main () { int n = 121; int temp = rev(n, 0); if (temp == n) Console.WriteLine( "yes" ); else Console.WriteLine( "no" ); } } // This code is contributed // by anuj_67. |
PHP
<?php // Recursive PHP program to check // if the number is palindrome or not // Recursive function that returns // the reverse of digits function rev( $n , $temp ) { // base case if ( $n == 0) return $temp ; // stores the reverse of a number $temp = ( $temp * 10) + ( $n % 10); return rev( $n / 10, $temp ); } // Driver Code $n = 121; $temp = rev( $n , 0); if ( $temp != $n ) echo "yes" ; else echo "no" ; // This code is contributed // by Sach_Code ?> |
Javascript
<script> // Recursive Javascript program to check if the // number is palindrome or not // recursive function that returns the reverse of digits function rev(n, temp) { // base case if (n == 0) return temp; // stores the reverse of a number temp = (temp * 10) + (n % 10); return rev(Math.floor(n / 10), temp); } // Driver Code let n = 121; let temp = rev(n, 0); if (temp == n) document.write( "yes" + "<br>" ); else document.write( "no" + "<br>" ); // This code is contributed by Mayank Tyagi </script> |
输出:
yes
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END