I am having a problem trying to write a program that gets numbers from a selected file and returns each numbers sum of its perfect divisors. Could anyone give me a few pointers??

#include <stdio.h> /* define fopen,fclose,fscanf,fprintf, EOF */

int sum_of_divisors(int num);
        /* pre: Accepts the integer */
        /* post: Returns the sum of its perfect divisors */

int main(void)
{
 FILE *inp;             /* input file pointer */
                        /* output file pointer */

 int num;               /* number read from the input file */
 int factorial_value;   /* value returned by the function */

 inp = fopen("perfect.dat","r");

 factorial_value = int sum_of_divisors(int num);
 printf("The factorial value of the %d is %d",num,factorial_value);

return (0);
}

int sum_of_divisors(int num)
{
 int sum = 0; 
 int i = 1;
  for(i; i < num; i++){
   if((num % i) == 0){
   sum += i;
  }
}
  return (sum);
}

Recommended Answers

All 3 Replies

Member Avatar for iamthwee

First of all figure out to:

1)Read your data from your file.
2)Do this without functions first for simplicity.
3)Give us an example of your text file and what you hope your output to look like.

I am having a problem trying to write a program that gets numbers from a selected file and returns each numbers sum of its perfect divisors. Could anyone give me a few pointers??

A few modificationes to your code and you're there.
Read the extra comments.

#include <stdio.h> /* define fopen,fclose,fscanf,fprintf, EOF */

int sum_of_divisors(int num); /* prototype */
 
 int main ( void )
 {
 FILE *inp;             /* input file pointer */
                        /* output file pointer */

 /* int num; ( declared inside while loop ) number read from the input file */
 int factorial_value;   /* value returned by the function */
 char buffer[300] = { 0 }; /* Needed for reading line from file */

 inp = fopen( "perfect.dat", "r" );
 /* check that file exist or could be open */
 if ( inp == NULL )
 {
        puts( "Error opening file" );
        puts( "Press enter to exit program" );
        getchar();
        return 1;
 } /* always check that files can be read or written upon */

 /* factorial_value = int sum_of_divisors(int num);  WRONG SYNTAX CALLING THE FUNCTION */
 
 /* Let's start getting numbers from file */
 while ( fgets( buffer, sizeof buffer / sizeof ( char ), inp ) != NULL )
 {
        int num;
        if ( sscanf( buffer, "%d", &num ) )
        {
            /* pass the scaned number to the function */
            factorial_value = sum_of_divisors( num );
            /* moving the printing inside the if block */
            printf( "The factorial value of the %d is %d\n",num,factorial_value );
        }
 }
 /* close file when not need it any more */
 fclose( inp );
 getchar(); /* pausing program */
 
 return (0);
}
/*
 * sum_of_divisisor function:
 * pre: Accepts the integer 
 * post: Returns the sum of its perfect divisors 
 */
int sum_of_divisors( int num )
{
     int sum = 0; 
     int i = 1;
     for(i; i < num; i++) {
          if((num % i) == 0) {
               sum += i;
          }
     }
     return (sum);
}

I made a few adjustments. and after my assignment I listed the input file with a mixture of perfect numbers and non perfect numbers.

#include <stdio.h> /* define fopen,fclose,fscanf,fprintf, EOF */
 
int sum_of_divisors(int num);
        /* pre: Accepts the integer */
        /* post: Returns the sum of its perfect divisors */
 
int main(void)
{
 FILE *inp,             /* input file pointer */
      *outp;            /* output file pointer */

 int num;               /* number read from the input file */
 int sum;               /* value returned by the function */

 inp = fopen("perfect.dat","r");
 outp = fopen("results.dat","w");
 
 factorial_value = int sum_of_divisors(int num);
 printf("The sum of %d perfect devisors is %d",num,sum);

return (0);
}
 
 
int sum_of_divisors(int num)
{
 int sum = 0;
 int i = 1;
  for(i; i < num; i++){
   if((num % i) == 0){
   sum += i;
  }
}
  return (sum);
}

INPUT FILE
456
18
30642
8128
109
81
496
2412
11213
830
4253
7

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.