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 15:09:55 浏览: 171
以下是 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 变量中。我们使用整数除法运算符(//)和取模运算符(%)来获取数字的每个位数。
最后,我们检查原始数字和反转数字是否相等。如果相等,则该数字是回文数字,否则不是回文数字。
相关问题
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.
Write the methods with the following headers // Return the reversal of an integer, i.e., reverse(456) returns 654 int reverse(int number) // Return true if number is a palindrome int isPalindrome(int number) Use the reverse method to implement isPalindrome. A number is a palindrome if its reversal is the same as itself. Write a test program that prompts the user to enter an integer and reports whether the integer is a palindrome.
Sure! Here are the methods you requested:
```java
public static int reverse(int number) {
int reversedNumber = 0;
while (number != 0) {
reversedNumber = reversedNumber * 10 + number % 10;
number /= 10;
}
return reversedNumber;
}
public static boolean isPalindrome(int number) {
return number == reverse(number);
}
```
And here's a program that uses these methods to check if a user-entered integer is a palindrome:
```java
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
if (isPalindrome(number)) {
System.out.println(number + " is a palindrome");
} else {
System.out.println(number + " is not a palindrome");
}
}
}
```
Hope this helps! Let me know if you have any further questions.
阅读全文