I am trying to develop a program that reads a list of integers from a file and outputs whether or not each integer in the file is perfect. Its suppose to give practice using endfile-controlled and for loops. The input file of numbers i have created are :
        456
        18
        30642
        8128
        109
        81
        496
        2412
        11213
        830
        4253
        7
#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 factorial_value;   /* 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 factorial value of the %d is %d",num,factorial_value);

return (0);
}


int sum_of_divisors(int num)
{
 int sum = 0;
 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 2 Replies

I am trying to develop a program that reads a list of integers from a file and outputs whether or not each integer in the file is perfect. Its suppose to give practice using endfile-controlled and for loops.

I'd start by attempting something. Just opening a file and not checking the error status does not even attempt to practice EOF-controlled loops. Did you forget what you read in the post titled Read Me: Read This Before Posting?

I'd start by attempting something. Just opening a file and not checking the error status does not even attempt to practice EOF-controlled loops.

He/she is in ignoring mode. That code was already corrected in the most basic errors, in this post.

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.