k can be 0, so you can’t write d%k or n%k, you can read more about sigfpe error on this Why do I get a SIGFPE?