Contact Learn C
Copy

Program 205:To print Sum of integers in File

Program 205:
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 main ()
 {
    FILE *file;
    int num[100],i=0,sum=0;
    file=fopen("file4sum.txt","r");

     if(file==NULL)
     {
      printf("File Not Found\n");
      exit(0);
     }
     else
     {
      while(fscanf(file,"%d",&num[i])!=EOF)
      {
       i++;
      }
     }
     
     for(i=0;i<10;i++)
     {
      sum+=num[i];
     }
    printf("Sum=%d\n",sum);
    fclose(file);
 }
Explanation:

//Coming Soon...

Output:

Store both text file and program in same folder.Here I named text file as file4sum.txt

Running the Program






Integers in File



 
Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide