Ask Question
29 November, 20:58

Write a program that first calls a function to calculate the square roots of all numbers from 1 to 1,000,000 and write them to a file named: roots. txt (See output on page 3) 2.) Write a program then calls a function to calculate the squares of all numbers from 1 to 1,000,000 and write them to a file named: squares. txt 3.) Here are the function prototypes: void writeRoots (); void writeSquares ();

+4
Answers (1)
  1. 29 November, 21:09
    0
    The csharp program to compute square roots of numbers and write to a file is as follows.

    class Program

    {

    static long max = 1000000;

    static void writeRoots ()

    {

    StreamWriter writer1 = new StreamWriter ("roots. txt");

    double root;

    for (int n=1; n<=max; n++)

    {

    root = Math. Sqrt (n);

    writer1. WriteLine (root);

    }

    }

    static void Main (string[] args)

    {

    writeRoots ();

    }

    }

    The program to compute squares of numbers and write to a file is as follows.

    class Program

    {

    static long max = 1000000;

    static void writeSquares ()

    {

    StreamWriter writer2 = new StreamWriter ("squares. txt");

    long root;

    for (int n = 1; n < = max; n++)

    {

    root = n*n;

    writer2. WriteLine (root);

    }

    }

    static void Main (string[] args)

    {

    writeSquares ();

    }

    }

    Explanation:

    1. An integer variable, max, is declared to hold the maximum value to compute square root and square.

    2. Inside writeRoots () method, an object of StreamWriter () class is created which takes the name of the file as parameter.

    StreamWriter writer1 = new StreamWriter ("roots. txt");

    3. Inside a for loop, the square roots of numbers from 1 to the value of variable, max, is computed and written to the file, roots. txt.

    4. Similarly, inside the method, writeSquares (), an object of StreamWriter () class is created which takes the name of the file as parameter.

    StreamWriter writer2 = new StreamWriter ("squares. txt");

    5. Inside a for loop, the square of numbers from 1 to the value of the variables, max, is computed and written to the file, squares. txt.

    6. Both the methods are declared static.

    7. Inside main (), the method writeRoots () and writeSquares () are called in their respective programs.

    8. Both the programs are written using csharp in visual studio. The program can be tested for any operation on the numbers.

    9. If the file by the given name does not exists, StreamWriter creates a new file having the given name and then writes to the file using WriteLine () method.

    10. The csharp language is purely object-oriented language and hence all the code is written inside a class.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a program that first calls a function to calculate the square roots of all numbers from 1 to 1,000,000 and write them to a file ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers