2. Write a program to check if the given number is a palindrome number. A palindrome number is a number that is same after reverse. For example 545, is the palindrome numbers.
时间: 2023-06-23 08:09:55 浏览: 58
以下是 Python 代码实现:
```python
num = int(input("请输入一个数字: ")) # 获取输入的数字
temp = num # 保存原始数字
reverse_num = 0 # 用于存储反转后的数字
while num > 0:
digit = num % 10
reverse_num = reverse_num * 10 + digit
num = num // 10
if temp == reverse_num:
print("是回文数字")
else:
print("不是回文数字")
```
首先,我们从用户输入数字开始。我们将原始数字存储在一个变量中,并将另一个变量初始化为0,以便在反转数字时使用。
我们使用 while 循环从数字的最后一位开始迭代,然后将该数字添加到 reverse_num 变量中。我们使用整数除法运算符(//)和取模运算符(%)来获取数字的每个位数。
最后,我们检查原始数字和反转数字是否相等。如果相等,则该数字是回文数字,否则不是回文数字。
相关问题
Given an integer with an arbitrary number of numerical digits, write a function to check iteratively if the integer is a palindrome, e.g., 5679765.且要使用 using namespace std;
Sure! Here's an example function that checks if an integer is a palindrome:
```c++
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(int num) {
string str = to_string(num);
int start = 0, end = str.length() - 1;
while (start < end) {
if (str[start] != str[end]) {
return false;
}
start++;
end--;
}
return true;
}
int main() {
int num = 5679765;
if (isPalindrome(num)) {
cout << num << " is a palindrome" << endl;
} else {
cout << num << " is not a palindrome" << endl;
}
return 0;
}
```
The function `isPalindrome` converts the integer to a string, and then iteratively checks if the characters at the start and end of the string match. If they don't match, the function returns `false`. If the function completes the loop without finding any mismatches, it returns `true`.
Hope this helps!
A palindrome string is a string that reads the same backward as forward. For example, "racecar" is a palindrome string because it reads the same way from left to right and from right to left. Another example is "level". Write a C program to check whether a given string is a palindrome or not.
```
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int i, j, len;
int flag = 1; // flag = 1 means the string is a palindrome, flag = 0 means the string is not a palindrome
printf("Enter a string: ");
scanf("%s", str);
len = strlen(str);
for (i = 0, j = len - 1; i < len / 2; i++, j--)
{
if (str[i] != str[j])
{
flag = 0;
break;
}
}
if (flag == 1)
{
printf("%s is a palindrome.\n", str);
}
else
{
printf("%s is not a palindrome.\n", str);
}
return 0;
}
```
Explanation:
1. Declare a character array `str` to store the input string.
2. Declare integer variables `i`, `j`, and `len` to use in the for loop.
3. Declare an integer variable `flag` to indicate whether the string is a palindrome or not.
4. Use `printf` function to prompt the user to enter a string and use `scanf` function to read the input string.
5. Use `strlen` function to calculate the length of the input string and store it in `len`.
6. Use a for loop to check if the string is a palindrome. The loop runs from the beginning of the string to the middle of the string (len/2). The variable `i` starts from the beginning of the string and `j` starts from the end of the string. At each iteration, we compare the i-th character from the beginning of the string with the j-th character from the end of the string. If they are not equal, set `flag` to 0 and break out of the loop.
7. If `flag` is still 1, output that the string is a palindrome. Otherwise, output that the string is not a palindrome.