How to manipulate large numbers in C? -
i wrote algorithm check if number prime , numbers divide it. want know 600851475143 divisors (for example) , outputs negative number. that's code:
#include <stdio.h> /* discover numbers divide 1 read input, show if it's prime */ int main() { long checker; long step = 1; int divisors = 0; printf("enter number want know divisors: "); scanf("%d", &checker); while(step <= checker){ /* check numbers divide number read*/ if (checker % step == 0) { printf("%d divides %d\n", step, checker); step +=1; divisors +=1; } else{ step+=1; } } /*now check if prime*/ if (divisors == 2) { printf("additional info: %d prime number.\n", checker); } else { printf("additional info: %d not prime number.\n", checker); } printf("done!\n"); return 0; }
the problem scanf
long
using format string %d
expects address of int
variable. same goes way use printf
.
you need replace every %d
%ld
in format strings. need because size of int
, of long
not equal.
edit: gcc points out mistake using -wall
option.