Finding Abundant Numbers from txt file

This is a discussion on Finding Abundant Numbers from txt file within the C Programming forums, part of the General Programming Boards category; I have to Write a program that reads in a list of numbers from a file, and for each number, ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    12

    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

    Code:
          #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;
      
                  }
        
     
              fclose(ifp);
      
              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);
    
    
          system("pause");
    
          return 0;
    
          }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    What do you mean by an "abundant number"

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    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
    Posts
    8,868
    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, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21