Date created: Thursday, September 27, 2012 11:03:23 PM. Last modified: Thursday, December 13, 2012 11:41:20 AM

Problem 4

 

// Project Euler - Problem 4

#include  // For cout
#include  // For snprintf()
#include 

using namespace std;

int main() {

  const int ubound = 999, lbound = 100;
  const int anslen = 7; // 6 + NULL Byte
  int x = ubound, y = ubound, product, answer = 0;
  char forwards[anslen], backwards[anslen];

while (x>=lbound) {

  while (y>=lbound) {
    product = x * y;
    snprintf(forwards, anslen, "%d", product);
    int b = 0;
    for(int f = (anslen-2); f>=0; f--) {
      backwards[b] = forwards[f];
      b++;
    }
    backwards[b] = '\0';
    if (strcmp(forwards, backwards) == 0)
    {
      if(product > answer) { answer = product; }
    }

    y--;
  }

  y = ubound;
  x--;

}
  cout << answer << endl;
  return 0;
}

 


Previous page: Problem 3
Next page: Problem 5