179k views
3 votes
Write code that does the following: opens an output file with the filename numbers.txt , uses a loop to write the numbers 1 through 100 to the file, and then closes the file.

User Hronro
by
6.8k points

2 Answers

4 votes
#include <iostream> #include <fstream> using namespace std; int main( int argc, ... outputFile.open( "primeNumbers.txt" ); //

Maybe this will help :)
User Zwo
by
7.3k points
2 votes

Answer:

numbersFile=open("numbers.txt","w")

for i in range(1,101):

numbersFile.write("%s\\" % i)

numbersFile.close()

Step-by-step explanation:

The programming language used here is python,.

The first line opens a file numbers.txt in the write mode (w), which means you can add new lines of text to the file.

In the second line, the range function is used with a FOR loop to generate a range of numbers from 1 through 100.

In the third line, the program adds all the numbers generated by the range and FOR loop to the txt file, and finally the file is closed.

User Nalnpir
by
7.7k points