Hi guys, I wrote up a long program that pretty much works. What it does is when the user enters a string like 4,100 the program prints out all the prime,perfect and amicable pairs in that range.
Trouble is, I got the prime and perfect numbers to work but I'm having a bit of difficulty getting the amicable pairs.
Here is part of the code in main where the numbers from the user gets passed to the findAmicable method.
And here is the sum of the factors method and the findAmicable methods:Code:while (1) {
printf("Enter a command: ");
fgets(command, sizeof(command), stdin);
command[strlen(command) - 1] = '\0';
if (sscanf(command, "%d,%d", &A, &B) == 2) {
int i;
for (i = A; i < B; i++) {
findAmicable(i);
}
}
}
Help please? I don't know where the code is wrong.Code:int sumDiv(int number) {
int i;
int sum = 0;
for (i = 1; i <= number; i++) {
if(number % i == 0)
sum += i;
}
return sum;
}
void findAmicable (int number) {
int i;
int j;
for (i = 1; i <= number; i++) {
for (j = 1; j >= number; j--) {
if ((sumDiv(i) == j) && (sumDiv(j) == i) &&(i!=j)) {
printf("%d and %d are amicable numbers. \n", i, j);
}
}
}
}