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 is the meaning of c in c language?
write a C program: To search a file any word which starts with ?a?. If the word following this ?a? starts with a vowel.Then replace this ?a? with ?a? with ?an?. redirect with the output onto an output file.The source file and destination file are specified by the user int the command line.
Write a program to print factorial of given number without using recursion?
Why do we need functions in c?
What is multidimensional arrays
a) Identify the following declarations. Ex. int i (integer variable) float a[l0](array of 10 real nos) int (*f())() void *f int (*f()) [] void *f int f[] [] [] char *(*f) () int (*f[]) [] float(*f) [] [] float **f int ******f
What is a stream?
What does *p++ do?
the process of defining something in terms of itself is called (or) in C it is possible for the functions to call themselves. A function called a) nested function b) void function c) recursive function d) indifinite function
write a c program to find the sum of five entered numbers using an array named number
What are the disadvantages of c language?
Can we use visual studio for c?
Why cant I open a file by its explicit path?
How can I manipulate individual bits?
the real constant in c can be expressed in which of the following forms a) fractional form only b) exponential form only c) ascii form only d) both a and b