0

The i variable is causing my program to crash and it'll work without it but I'm not understanding why and if I replace with any other number it'll work but not do what I'd like.

    void longest_prime_factor(int n){

        for (int i=0;i<n;i++){

            if (n % i == 0) printf("true\n");
        }

   }
    longest_prime_factor(17);

Output

0 [main] primefactors 238 cygwin_exception::open_stackdumpfile: Dumping stack trace to primefactors.exe.stackdump

1 Answers1

2

The conditional in

if (n % i == 0) printf("true\n");

tries to calculate

n % 0

in the first iteration, which results in a floating point exception. Start with i = 1.

Ctx
  • 18,090
  • 24
  • 36
  • 51