Print all the palindrome numbers.If a number is not
palindrome make it one by attaching the reverse to it.
eg:123
output:123321 (or) 12321
Answer Posted / abdur rab
#include <stdio.h>
int reverse_digits ( int number, int* digits )
{
int n_digits = 0;
int rev_number = 0;
while ( number ) {
rev_number *= 10;
rev_number += number % 10;
number /= 10;
n_digits++;
}
*( digits ) = n_digits;
return ( rev_number );
}
int palindrome ( int number, int digits )
{
int flag = 1;
while ( number ) {
if ( ( number % 10 ) != ( number / (int)
pow ( 10, digits - 1 ) ) && ( digits > 1 ) ) {
flag = 0;
break;
}
// removing first and last digits
if ( 1 < ( digits -= 2 ) ) {
number /= 10;
number = number % (int) pow ( 10,
digits );
} else break;
}
return ( flag );
}
int main ( int argc, char* argv [] )
{
int number = 123;
int digits = 0;
int rev_number = 0;
rev_number = reverse_digits ( number, &digits );
if ( !palindrome ( number, digits ) ) {
printf ("\n NOT Palindrome, creating
Palindrome");
/**
* if you need 12321 use digits - 1
* if you need 123321 use digits
*/
number *= (int) pow ( 10, ( digits - 1 ) );
number += rev_number % (int) pow ( 10, (
digits - 1 ) );
}
printf ( "\n The Number: %d", number );
return ( 0 );
}
| Is This Answer Correct ? | 8 Yes | 12 No |
Post New Answer View All Answers
What does 3 mean in texting?
What is the difference between c &c++?
Are there any problems with performing mathematical operations on different variable types?
What do you understand by friend-functions? How are they used?
Write a program to swap two numbers without using the third variable?
Is c easy to learn?
How can you tell whether two strings are the same?
What is bubble sort in c?
What is string in c language?
What is indirection in c?
When reallocating memory if any other pointers point into the same piece of memory do you have to readjust these other pointers or do they get readjusted automatically?
Which operators cannot be overloaded a) Sizeof b) .* c) :: d) all of the above
Why ca not I do something like this?
What does stand for?
What is infinite loop?