使用 C++ 在給定範圍內列印所有迴文數字的程式
在此教程中,我們將討論一個在給定範圍內列印所有迴文數字的程式。
為此,我們將獲得要查找回文數字的數學範圍。我們的任務是在該範圍內找到所有迴文數字並將其打印出來。
示例
#include<iostream> using namespace std; //checking if the number is a palindrome int is_palin(int n){ int rev = 0; for (int i = n; i > 0; i /= 10) rev = rev*10 + i%10; return (n==rev); } void countPal(int min, int max){ for (int i = min; i <= max; i++) if (is_palin(i)) cout << i << " "; } int main(){ countPal(99, 250); return 0; }
輸出
99 101 111 121 131 141 151 161 171 181 191 202 212 222 232 242
廣告