用while循环查找回文数的C程序
回文数是反转时保持不变的数。在C语言中,允许用户输入任何正整数并使用while循环检查给定的数字是否为回文数。
示例1
以下是使用while循环查找回文数的C程序-
#include输出结果int main(){ int num, temp, rem, rev = 0; printf("enter a number:\n"); scanf("%d", &num); temp = num; while ( temp > 0){ rem = temp %10; rev = rev *10+ rem; temp = temp /10; } printf("reversed number is = %d\n", rev); if ( num == rev ) printf("\n%d is Palindrome Number.\n", num); else printf("%d is not the Palindrome Number.\n", num); return 0; }
执行上述程序时,会产生以下结果-
enter a number: 1234 reversed number is = 4321 1234 is not the Palindrome Number. enter a number: 1221 reversed number is = 1221 1221 is Palindrome Number.
例2
考虑另一个C程序示例,它通过对字符串使用while循环来查找回文数。
#include输出结果#include void pal(char string[]); int main(){ char string[100]; printf("输入一个字符串: "); gets(string); pal(string); return 0; } void pal(char string[]){ int i = 0; int length = strlen(string) - 1; while (length > i){ if(string[i++] != string[length--]){ printf("\n %s is not a palindrome", string); return; } } printf("\n %s is a palindrome string", string); }
执行上述程序时,会产生以下结果-
输入一个字符串: tutorial tutorial is not a palindrome 输入一个字符串: saas saas is a palindrome string
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下