Answer:
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.