Thread: Why isn't this working?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    22

    Why isn't this working?

    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.

    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);
                            }
                     }
    }
    And here is the sum of the factors method and the findAmicable methods:

    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);
    			}
    		}
    	}
    }
    Help please? I don't know where the code is wrong.
    Last edited by Watabou; 04-21-2011 at 12:09 AM. Reason: changed sumdivisors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-30-2009, 04:09 AM
  2. cin.get() Not working
    By fhbwghads in forum C++ Programming
    Replies: 3
    Last Post: 11-29-2008, 10:15 AM
  3. Working up to an FFT
    By lordbubonicus in forum C Programming
    Replies: 14
    Last Post: 12-06-2007, 09:59 PM
  4. Min Max not working
    By B0bDole in forum C Programming
    Replies: 5
    Last Post: 06-10-2005, 10:58 PM
  5. working out...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 04-10-2003, 10:20 AM