-1

Program not working, not giving output, I don't know what to do, where the problem is.

I'm trying to find out the largest palindrome made from the product of two 3-digit numbers.

#include <stdio.h>

main() {
  int i, k, j, x;
  long int a[1000000], palindrome[1000000], great, sum = 0;
  // for multiples of two 3 digit numbers
    for (k = 0, i = 100; i < 1000; i++) {
      for (j = 100; j < 1000; j++) {
        a[k] = i * j; // multiples output
          k++;
      }
    }

  for (i = 0, x = 0; i < 1000000; i++) {
    // for reverse considered as sum
      for (; a[i] != 0;) {
        sum = sum * 10 + a[i] % 10;
      }
    // for numbers which are palindromes 
      if (sum == a[i]) {
        palindrome[x] = a[i];
        x++;
        break;
      }
  }
  // comparison of palindrome number for which one is greatest
    great = palindrome[0];
  for (k = 0; k < 1000000; k++) {

    if (great < palindrome[k]) {
      great = palindrome[k];
    }
  }
  printf("\ngreatest palindrome of 3 digit multiple is : ", great);
}

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 1
    Define 'not working'. Did you try using a debugger? – CinCout Dec 13 '19 at 06:53
  • 4
    `long int a[1000000],palindrome[1000000]` Stackoverflow. Did you check for stack limitation on your machine? – kiran Biradar Dec 13 '19 at 06:53
  • 2
    This is not a Debug For Me Service. Take a look at [How-To-Ask](https://stackoverflow.com/help/how-to-ask) – LPs Dec 13 '19 at 07:13
  • 2
    Since you are trying to find only the largest palindrome, run the loop in reverse 999 to 100 and check the product is palindrome or not. Once you find the palindrome, break the loop. – Wander3r Dec 13 '19 at 07:33
  • 3
    Rip the code up and throw it away. Sit down and think about the problem before you start to write another code. What's the largest number which is the product of 2 3-digit numbers ? What's the largest palindrome with 6 digits ? Use what you know about the problem. – High Performance Mark Dec 13 '19 at 07:34
  • 1
    `printf("\ngreatest palindrome of 3 digit multiple is : ",great);` ---> `printf("\ngreatest palindrome of 3 digit multiple is : %ld\n",great);` – LPs Dec 13 '19 at 07:36

2 Answers2

0

What do you mean with "not working"?

There are two things, from my point of view:

1) long int a[1000000], palindrome[1000000]

Depending on you compile configuration you could have problems compiling your code. Probably the array is too big to fit in your program's stack address space. In C or C++ local objects are usually allocated on the stack. Don't allocate it local on stack, use some other place instead. This can be achieved by either making the object global or allocating it on the global heap.

#include <stdio.h>

long int a[1000000], palindrome[1000000], great, sum = 0;

main() {
  int i, k, j, x;

2) printf("\ngreatest palindrome of 3 digit multiple is : ", great);

I will change it by :

printf("\ngreatest palindrome of 3 digit multiple is %li: ", great);

Regards.

omotto
  • 1,721
  • 19
  • 20
0

Compiling and running your code on an on-line compiler I got this:

prog.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main() {
^
prog.c:34:61: warning: data argument not used by format string [-Wformat-extra-args]
  printf("\ngreatest palindrome of 3 digit multiple is : ", great);
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  ^
2 warnings generated.
Killed

Both the warnings should be taken into account, but I'd like to point out the last line. The program was taking too much time to run, so the process was killed.

It's a strong suggestion to change the algorithm, or at least to fix the part that checks if a number is a palindrome:

for (; a[i] != 0;) {            // <--   If a[i] is not 0, this will never end
    sum = sum * 10 + a[i] % 10; 
}

I'd use a function like this one

bool is_palindrome(long x)
{
    long rev = 0;
    for (long i = x; i; i /= 10)
    {
        rev *= 10;
        rev += i % 10;
    }
    return x == rev;
}

Also, we don't need any array, we could just calculate all the possible products between two 3-digits number using two nested for loops and check if those are palindromes.

Starting from the highest numbers, we can store the product, but only if it's a palindrome and is bigger than any previously one found, and stop the iteration of the inner loop as soon as the candidate become less then the stored maximum. This would save us a lot of iterations.

Implementing this algorithm, I found out a maximum value of 906609.

Bob__
  • 12,361
  • 3
  • 28
  • 42