Back to Problem list
Back to Problem list
Problem 17: Write a program to print all paindromic number from 1 to n. First few palindromic numbers are: 1 2 3....9..11,22,...131....1225221.
Solution:
Our main task is to determine whatever a number is palindromic or not. We know that a palindromic number or numeral palindrome is a 'symmetrical' number like 16461, that remains the same when its digits are reversed.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int isPalin(int n) {
char str[100];
sprintf(str,"%d",n); // converting a number to a string
strrev(str); // reversing the string
int m = atoi(str); // converting the reversed string to an integer
return n == m; // comparing reversed number with orginal number, retrun 1 when true, 0 otherwise
}
void printPalin(int n) {
int i;
for(i = 1; i<=n; i++) {
if(isPalin(i)) printf("%d ",i);
}
}
int main() {
int n;
scanf("%d",&n);
printPalin(n);
return 0;
}
|
Back to Problem list