Thread: Finding Abundant Numbers from txt file

  1. #1
    Registered User
    Join Date
    Sep 2009

    Finding Abundant Numbers from txt file

    I have to Write a program that reads in a list of numbers from a file, and for each number, determines and prints out whether or not that number is abundant.

    Ive got this code so far...i cant get it to work. I just put randon numbers in the txt file

          #include <stdio.h>
            int main(void){
            FILE *ifp= fopen("abundant.txt", "r");
            int numcases;
            int i;
            int j;
            int num;
            int divisor; // this will be used to calculate the sum of the divisors
            int sum =0;
            fscanf(ifp, "%d", &numcases);
              for (i=1; i<= numcases; i++){
                 for (j=1; j <= (num/2); j++)
                     fscanf(ifp, "%d", &num);
                     divisor = num % j;
                 if (divisor == 0)
                     sum= sum+j;
              if (sum > num)
                 printf("Test Case #%d: %d is an abundant number.\n", numcases, num);
                 else printf("Test Case #%d: %d is not an abundant numnber.\n", numcases, num);
          return 0;

  2. #2
    Registered User
    Join Date
    Oct 2008
    What do you mean by an "abundant number"

  3. #3
    Registered User
    Join Date
    Sep 2009
    numbers with proper divisors that sum to greater than the number itself. These numbers are called, “abundant numbers.” A proper divisor of a number is any number strictly less than it that divides evenly into it. For example, 6 is a proper divisor of 12, but 12 is not. 12 is an example of an abundant number because 1+2+3+4+6 = 16 and 16 is greater than 12. As this example shows, one way to find out all the proper divisors of a number is to try each integer (starting at 1) out until you reach the number divided by 2. (It’s impossible for any number in between 7 and 11 to divide evenly into 12 because it is guaranteed that the result of the division is less than 2 since we are dividing by a greater number, and greater than 1, since we are not dividing by the number itself.)

  4. #4
    Registered User
    Join Date
    Sep 2006
    First, have you checked that you're getting the numbers written out, and read in OK?

    Second, numcases means what? That should be testcase or testnum or something else for a variable name. It should not control the stop on the for loop.

    Third, you're using num/2 as a stop control for the j loop, but num/2 was never given a value. Global numbers will be set to zero, but local int's - probably not. Check that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

Tags for this Thread