27.7k views
5 votes
Assume a file containing a series of integers is named numbers.txt and exists on the computer’s disk. Write a program that reads all of the numbers stored in the file, calculates their total and displays it.

Very Important: The file could have any number of numbers, so you need to use a loop to process the data in the file

User Will Webb
by
8.0k points

1 Answer

3 votes

#include <iostream>

#include <fstream>

using namespace std;

int main(){

int x, sum=0;

ifstream f("numbers.txt");

while(!f.eof()){

f >> x;

sum +=x;

}

cout << "Sum : " << sum;

}

This is a C++ program.

Next time be more precise on the programming language you want

User BharathBob
by
8.4k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.