-2

I am trying to give a shot at Project Euler problem 3 until codeblocks or whatever caused it pissed me off. This is my code, What is wrong with it? I guess more of a bug?

#include <iostream>
using namespace std;

int main()
{
    int x=0;

    for(int y=0;y<=10;y++)
    {
        if(13195%x==0)
        {
            cout<<"I don't know why the program crashes!";
        }
    }
}
Rohan Bojja
  • 655
  • 1
  • 16
  • 35

5 Answers5

2

You can't use a 0 as the second operand while doing / or %. What you're essentially saying is "Hey divide by 0 and give me the remainder." Please see the following:

Can't Mod Zero?

Community
  • 1
  • 1
The Vanilla Thrilla
  • 1,915
  • 10
  • 30
  • 49
2

Modulus operator divides it by zero and next finds the remainder, thus you will get divide by zero error

prakashb
  • 160
  • 1
  • 8
1

x must not be equal to 0 otherwise division by zero.

Just think how many zeroes in 13195?

Yola
  • 18,496
  • 11
  • 65
  • 106
0

x = 0. Dividing a number with zero will crash your code. Make sure x is not 0 before 13195 % x.

Summer_More_More_Tea
  • 12,740
  • 12
  • 51
  • 83
0

The operation A modulo B is defined as: the remainder of the division of A by B. In your code, you have B=0 which means you are trying to divide by zero.

syam
  • 14,701
  • 3
  • 41
  • 65