Steve Wesley
Steve Wesley

Reputation: 21

Calculate the two palindrome of number

I have one little project where i have to program one algorithm that calculates the two palindrome of a number. Per example : if the input is 367 the result is 367763 and 763367.

I know how to check if the number is a palindrome. But i have to create the two palindrome and it's a little different and i can't find the answer.

Please before unvote, i am a noob guy in programming that is hopping to learn something, i already tried to solve the problem and searched a lot.

Here is my code to check if number is palindrome:

    #include <stdio.h>
int main()
{
   int num, reverse_num=0, remainder,temp;
   printf("Enter an integer: ");
   scanf("%d", &num);
   temp=num;
   while(temp!=0)
   {
      remainder=temp%10;
      reverse_num=reverse_num*10+remainder;
      temp/=10;
   } 
   if(reverse_num==num) 
      printf("%d is a palindrome number",num);
   else
      printf("%d is not a palindrome number",num);
   return 0;
}

Upvotes: 0

Views: 258

Answers (2)

user3078414
user3078414

Reputation: 1937

Your code already has both num and reverse_num. All you have to do is print them one after the other in your if else statement. That's for "instant gratification".

if(reverse_num==num) {
   printf("palindrome1 = palindrome2 = %d%d\n", num, num); 
}else{
   printf("palindrome1 = %d%d\n",num, reverse_num);
   printf("palindrome2 = %d%d\n",reverse_num, num);   
} 

If you really want to calculate the palindromes as actual integers instead, it is just a few more lines of code. Add one counter for keeping track of decimal places while computing reverse_num... We can easily post the code here, but it's a good and simple exercise in learning C and you'll get much more of it if you do it yourself.

Of course, there are other ways of achieving the result, as the other answer and comments suggest. This answer is based on your code input.

Please, also be aware that the numbers of form a*10^n, or more generally … + 0*10^0 may result in some "surprisingly looking" palindromes, at least in the terms how they are defined in the OP.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

Here you are.

#include <stdio.h>

struct palindrome_pair
{ 
    unsigned long long int first;
    unsigned long long int second;
};

struct palindrome_pair build_palindrome( unsigned int x )
{
    const unsigned int Base = 10;
    struct palindrome_pair palindrome = { x, 1 };

    unsigned int y = 0;

    for ( unsigned int value = x; value != 0; value /= Base )
    {
        y = Base * y + value % Base;

        palindrome.first  *= Base;
        palindrome.second *= Base;
    }

    palindrome.first += y;
    palindrome.second = palindrome.second * y + x;

    return palindrome;
}

int main( void ) 
{
    unsigned int x = 367u;

    struct palindrome_pair palindrome = build_palindrome( x );

    printf( "%u:\t%llu\t%llu\n\n", x, palindrome.first, palindrome.second );

    const unsigned int N = 20;

    for ( unsigned int i = 0; i <= N; i++ )
    {
        palindrome = build_palindrome( i );

        printf( "%u:\t%llu\t%llu\n", i, palindrome.first, palindrome.second );
    }

    return 0;
}

The program output is

367:    367763  763367

0:  0   0
1:  11  11
2:  22  22
3:  33  33
4:  44  44
5:  55  55
6:  66  66
7:  77  77
8:  88  88
9:  99  99
10: 1001    110
11: 1111    1111
12: 1221    2112
13: 1331    3113
14: 1441    4114
15: 1551    5115
16: 1661    6116
17: 1771    7117
18: 1881    8118
19: 1991    9119
20: 2002    220

Upvotes: 1

Related Questions