📜  TCS编码实践问题|检查回文数

📅  最后修改于: 2021-05-28 02:09:52             🧑  作者: Mango

给定一个数字,任务是使用命令行参数检查该数字是否为回文式。

例子:

Input: 123
Output: No

Input: 585
Output: Yes

方法:

  • 由于该数字是作为命令行参数输入的,因此不需要专用的输入行
  • 从命令行参数中提取输入数字
  • 提取的数字将为String类型。
  • 将此数字转换为整数类型并将其存储在变量中,例如num
  • 找到该数字的反面并存储在变量中,例如rev_num
  • 检查此rev_num和num是否相同。
  • 如果它们不相同,则数字不是回文
  • 如果相同,则为回文数

程序:

C
// C program to check if a number is Palindrome
// using command line arguments
  
#include 
#include  /* atoi */
  
// Function to reverse the number
int reverseNumber(int num)
{
  
    // Variable to store the
    // resultant reverse number
    int rev_num = 0;
  
    // Traverse through the number digit by digit
    while (num > 0) {
  
        // Append the last digit of num
        // as the next digit of rev_num
        rev_num = rev_num * 10 + num % 10;
  
        // Remove the last digit from the num
        num = num / 10;
    }
  
    // Return the reversed number
    return rev_num;
}
  
// Function to reverse a string
int isPalindrome(int num)
{
    int rev_num = reverseNumber(num);
  
    if (num == rev_num)
        return 1;
    else
        return 0;
}
  
// Driver code
int main(int argc, char* argv[])
{
  
    int num, res = 0;
  
    // Check if the length of args array is 1
    if (argc == 1)
        printf("No command line arguments found.\n");
  
    else {
  
        // Get the command line argument and
        // Convert it from string type to integer type
        // using function "atoi( argument)"
        num = atoi(argv[1]);
  
        // Check if it is Palindrome
        res = isPalindrome(num);
  
        // Check if res is 0 or 1
        if (res == 0)
            // Print No
            printf("No\n");
        else
            // Print Yes
            printf("Yes\n");
    }
    return 0;
}


Java
// Java program to check if a number is Palindrome
// using command line arguments
  
class GFG {
  
    // Function to reverse the number
    public static int reverseNumber(int num)
    {
  
        // Variable to store the
        // resultant reverse number
        int rev_num = 0;
  
        // Traverse through the number digit by digit
        while (num > 0) {
  
            // Append the last digit of num
            // as the next digit of rev_num
            rev_num = rev_num * 10 + num % 10;
  
            // Remove the last digit from the num
            num = num / 10;
        }
  
        // Return the reversed number
        return rev_num;
    }
  
    // Function to reverse a string
    public static int isPalindrome(int num)
    {
        int rev_num = reverseNumber(num);
  
        if (num == rev_num)
            return 1;
        else
            return 0;
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Check if length of args array is
        // greater than 0
        if (args.length > 0) {
  
            // Get the command line argument and
            // Convert it from string type to integer type
            int num = Integer.parseInt(args[0]);
  
            // Get the command line argument
            // and check if it is Palindrome
            int res = isPalindrome(num);
  
            // Check if res is 0 or 1
            if (res == 0)
                // Print No
                System.out.println("No\n");
            else
                // Print Yes
                System.out.println("Yes\n");
        }
        else
            System.out.println("No command line "
                               + "arguments found.");
    }
}


输出:

  • 在C中:
  • 在Java:

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。