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 nil mean in c?
What should malloc(0) do?
What are near, far and huge pointers?
Write a C Program That Will Count The Number Of Even And Odd Integers In A Set using while loop
What is the advantage of a random access file?
What is c++ used for today?
What are inbuilt functions in c?
a program that can input number of records and can view it again the record
What is the size of a union variable?
What are linked lists in c?
write a program that will open the file, count the number of occurences of each word in the the complete works of shakespeare. You will then tabulate this information in another file.
Array is an lvalue or not?
Is struct oop?
Explain what is the difference between a string and an array?
explain what is a newline escape sequence?