K Palindromic Integers Program In Python
Find the first K palindromic integers greater than a given number N.
Understand the Problem
Problem Statement
The program must accept two integers N and K as the input. The program must print the first K palindromic integers greater than N as the output.
Constraints
- 1 <= N <= 10^7
- 1 <= K <= 1000
- Output integers must be strictly greater than N
- Output integers must be exactly K in count
- Palindromic integers read the same forwards and backwards
Examples
1221 51331 1441 1551 1661 1771Starting from 1222, the first 5 palindromic integers are 1331, 1441, 1551, 1661, and 1771. These numbers read the same forwards and backwards.
658100 10658856 659956 660066 661166 662266 663366 664466 665566 666666 667766Starting from 658101, we search sequentially and find these 10 consecutive palindromic integers. Each number is greater than 658100 and reads the same forwards and backwards.
Solution
#include <stdio.h>
#include <stdbool.h>
bool isPalindrome(int num) {
int original = num;
int reversed = 0;
while (num > 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
return original == reversed;
}
int main() {
int N, K;
scanf("%d %d", &N, &K);
int count = 0;
int current = N + 1;
while (count < K) {
if (isPalindrome(current)) {
printf("%d ", current);
count++;
}
current++;
}
return 0;
}C Solution Explanation:
1. isPalindrome function: Reverses the number by extracting digits and building the reversed number
2. Main function reads N and K
3. Starts checking from N + 1
4. For each number, calls isPalindrome to check if it reads the same forwards and backwards
5. When a palindrome is found, prints it and increments the count
6. Continues until K palindromic integers are found
7. Uses integer arithmetic to check palindromes without string conversion