I give up, I can't find a flaw in my code.
It's supposed to "find the greatest product of five consecutive digits in the 1000-digit number." The output is 31752, which doesn't appear to be correct.
#include <iostream>
#include <sstream>
usingnamespace std;
int main()
{
string x= "731671765313306249...963450"; // the number is so long it doesn't appear to fit here
int sum=1, n=0; int y, max=0; int i=0;
for (i=0; i<=999; i=i+5) {
for (n=i; n<=i+4; n++) {
stringstream convert;
convert << x[n];
convert >> y;
sum=sum*y;}
if (sum>max) max=sum;
sum=1;
}
cout << max;
}
I would like to mention you should work on proper indentations. inside each curly brace it the code moves over one tab ( 4-6 spaces ). Also you should get in the habit with using < in your for loops instead of <= there are very seldom cases where you should use <= instead of < it will help with arrays. Also shouldn't it be product and not sum? Also on line 15 what happens if it is they are equal? It will be reset.
Here is how I did this problem a long time ago but feel free to look at it.