检查数字是否为回文数

C++C++Beginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,我们将学习如何检查一个数字是否是回文数,并编写一个 C++ 程序来实现这一功能。回文数(palindrome number)是指反转后与原数相同的数字。例如,121、34543、343、131、48984 都是回文数。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp(("`C++`")) -.-> cpp/IOandFileHandlingGroup(["`I/O and File Handling`"]) cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/strings("`Strings`") cpp/ControlFlowGroup -.-> cpp/conditions("`Conditions`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") cpp/IOandFileHandlingGroup -.-> cpp/output("`Output`") cpp/IOandFileHandlingGroup -.-> cpp/user_input("`User Input`") cpp/SyntaxandStyleGroup -.-> cpp/code_formatting("`Code Formatting`") subgraph Lab Skills cpp/variables -.-> lab-96176{{"`检查数字是否为回文数`"}} cpp/strings -.-> lab-96176{{"`检查数字是否为回文数`"}} cpp/conditions -.-> lab-96176{{"`检查数字是否为回文数`"}} cpp/for_loop -.-> lab-96176{{"`检查数字是否为回文数`"}} cpp/function_parameters -.-> lab-96176{{"`检查数字是否为回文数`"}} cpp/output -.-> lab-96176{{"`检查数字是否为回文数`"}} cpp/user_input -.-> lab-96176{{"`检查数字是否为回文数`"}} cpp/code_formatting -.-> lab-96176{{"`检查数字是否为回文数`"}} end

包含头文件

在这一步中,我们将为程序包含所需的头文件。

#include<bits/stdc++.h>
using namespace std;

定义检查回文数的函数

在这一步中,我们将定义一个函数来检查数字是否为回文数。

void check_palindrome(int a){
    int temp=a;
    int res=0;
    for(int i=a;i>0;i=i/10){
        res+=res*10+i%10;
    }
    if(temp==res){
        cout<<a<<" is a Palindrome number";
    }
    else{
        cout<<a<<" is not a Palindrome number";
    }
}

代码解释:

我们定义了一个函数 check_palindrome,它接受一个整数值作为参数。

我们定义了两个整型变量来保存数字。变量 temp 存储输入的数字,变量 res 存储输入数字的反转结果。

我们使用了一个循环来反转数字。

我们比较了 tempres 的值,并显示相应的消息。

从用户获取数字并调用 check_palindrome() 函数

在这一步中,我们将从用户那里获取数字,并调用 check_palindrome() 函数来检查该数字是否为回文数。

int main(){
    int num;
    cout<<"Enter the number that you want to check:-";
    cin>>num;
    check_palindrome(num);
    return 0;
}

代码解释:

我们声明了主函数 main

我们定义了一个整型变量 num 来保存用户输入。

我们提示用户输入数字。

我们使用 cin 语句获取用户输入。

我们调用了 check_palindrome(num) 函数,并将用户输入的 num 作为参数传递。

编译并运行程序

在这一步中,我们将编译并运行 C++ 程序,以检查给定的数字是否为回文数。运行以下命令来编译并运行 C++ 程序:

g++ ~/project/main.cpp -o main && ./main

完整代码

将以下代码用于 main.cpp 文件:

#include<bits/stdc++.h>
using namespace std;

void check_palindrome(int a){
    int temp=a;
    int res=0;
    for(int i=a;i>0;i=i/10){
        res+=res*10+i%10;
    }
    if(temp==res){
        cout<<a<<" is a Palindrome number";
    }
    else{
        cout<<a<<" is not a Palindrome number";
    }
}

int main(){
    int num;
    cout<<"Enter the number that you want to check:-";
    cin>>num;
    check_palindrome(num);
    return 0;
}

总结

在本实验中,我们学习了如何使用 C++ 语言检查数字是否为回文数。我们按照以下步骤进行了操作:

  • 包含必要的头文件。
  • 定义用于检查回文数的函数。
  • 从用户获取数字作为输入,并调用函数来检查该数字是否为回文数。
  • 使用 g++ 命令编译程序并在终端中运行。

完成这些步骤后,你可以使用 C++ 语言检查任何给定的数字是否为回文数。

您可能感兴趣的其他 C++ 教程