Solution to Project Euler 8


Improve your writing skills in 5 minutes a day with the Daily Writing Tips email newsletter.

In Problem 8 you need to find the greatest product of five consecutive digits of a 1000-digit number that is given.

To find the solution I simply read the number into an array and then traversed it comparing the product of five digits at a time.

#include <stdio.h>

int main(){
  int vet[1000];
  int z,max,c,j,i=0;

  while(i<1000){
    c=getchar()-'0';
    if (c<0 || c>9)
      continue;
    else{
      vet[i]=c;
      i++;
    }

  }

  max=0;

  for (j=0;j<1000;j++){
    z=vet[j]*vet[j+1]*vet[j+2]*vet[j+3]*vet[j+4];
    if (z>max)
      max=z;

  }

  printf("%dn",max);

  return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *