Skip to content

Instantly share code, notes, and snippets.

@nullren
Created May 16, 2013 02:30
Show Gist options
  • Save nullren/5588993 to your computer and use it in GitHub Desktop.
Save nullren/5588993 to your computer and use it in GitHub Desktop.
/* Find the greatest product of five consecutive digits in the 1000-digit number.
*/
#include <stdio.h>
#include <string.h>
#define DIGITS 1000
#define SUBSEQUENCE_SIZE 5
int main()
{
int i=0,j,product,largest=0;
char numString[DIGITS];
char c=0;
FILE *fin = fopen("files/008.txt", "r");
while ((c = fgetc(fin)) != EOF)
if(c=='\n')
continue;
else
numString[i++] = c;
fclose(fin);
printf("numString is %s\n", numString);
for (i=0; i<=DIGITS-SUBSEQUENCE_SIZE; ++i)
{
product = 1;
for (j=0; j<SUBSEQUENCE_SIZE; ++j)
{
product *= numString[i+j] - '0';
}
largest = (product > largest ? product : largest);
}
printf("largest product is %d\n", largest);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment