C++ 程序:查找输入数字的反转

原文:https://www.studytonight.com/cpp-programs/cpp-find-the-reverse-of-the-entered-number

大家好!

在本教程中,我们将学习如何用 C++ 编程语言找到给定数字的倒数。

找到输入数字的反转的概念可以用来检查回文。

代号:

#include <iostream>
#include <math.h>

using namespace std;

//Returns the reverse of the entered number
int findReverse(int n)
{
    int reverse = 0; //to store the reverse of the given number
    int remainder = 0;

    //logic to compute the reverse of a number
    while (n != 0)
    {
        remainder = n % 10; //store the digit at the units place
        reverse = reverse * 10 + remainder;
        n /= 10;
    }

    return reverse;
}

int main()
{
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " ===== Program to compute the Reverse of the entered number. ===== \n\n";

    //variable declaration
    int n;
    int reverse = 0;

    //taking input from the command line (user)
    cout << " Enter a positive integer to find the reverse of :  ";
    cin >> n;

    //Calling a method that returns the reverse of an entered number
    reverse = findReverse(n);

    cout << "\n\nThe entered number is " << n << " and it's reverse is :" << reverse;

    cout << "\n\n\n";

    return 0;
}

输出:

C++ number reverse program output

我们希望这篇文章能帮助你更好地理解如何在 C++ 中找到给定数字的反转。如有任何疑问,请随时通过下面的评论区联系我们。

继续学习: