1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
SashulF [63]
4 years ago
8

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();
Computers and Technology
1 answer:
svlad2 [7]4 years ago
3 0

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.

You might be interested in
The Gas-N-Clean Service Station sells gasoline and has a car wash. Fees for the car wash are $1.25 with a gasoline purchase of $
Xelga [282]

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

              //print the header

              System.out.println("**************************************");

              System.out.println("*                                    *");

              System.out.println("*                                    *");

              System.out.println("*   Gas-N-Clean Service Station      *");

              System.out.println("*                                    *");

              System.out.println("*   March 2, 2004                    *");

              System.out.println("*                                    *");

              System.out.println("**************************************");

 

               //set the constant values for gasoline

               final double REGULAR_GASOLINE = 2.89;

               final double PLUS_GASOLINE = 3.09;

               final double SUPER_GASOLINE = 3.39;

 

              // initialize the variables as 0

              double gasolinePrice = 0, gasolineCost = 0, carWashCost = 0, totalCost = 0;

 

               Scanner input = new Scanner(System.in);

 

               //ask the user to enter the gallons, gas type and car wash choice

               System.out.print("Enter number of gallons ");

               double gasolinePurchase = input.nextDouble();

               System.out.print("Enter gas type (R, P, S, or N) ");

               char gasType = input.next().charAt(0);

               System.out.print("Enter Y or N for car wash ");

               char carWashChoice = input.next().charAt(0);

               //check the gas type. Depending on the choice set the gasolinePrice from the corresponding constant value

              if(gasType == 'R')

                  gasolinePrice = REGULAR_GASOLINE;

              else if(gasType == 'P')

                  gasolinePrice = PLUS_GASOLINE;

              else if(gasType == 'S')

                  gasolinePrice = SUPER_GASOLINE;

 

                //calculate the gasolineCost

                gasolineCost = gasolinePurchase * gasolinePrice;

 

               //check the carWashChoice. If it is yes and gasolineCost is greater than 10, set the carWashCost as 1.25. Otherwise, set the carWashCost as 3.00

               if(carWashChoice == 'Y'){    

                   if(gasolineCost >= 10)

                       carWashCost = 1.25;

                   else

                       carWashCost = 3.00;

               }

              //calculate the total cost, add gasolineCost and carWashCost

              totalCost = gasolineCost + carWashCost;

 

               //print the values in required format

              System.out.println("Amount Gasoline purchases " + gasolinePurchase);

              System.out.println("Gallons Price per gallons $ " + gasolinePrice);

              System.out.printf("Total gasoline cost $ %.2f\n", gasolineCost);

              System.out.println("Car wash cost $ " + carWashCost);

              System.out.printf("Total due $ %.2f\n", totalCost);

               System.out.println("Thank you for stopping\nPlease come again \nRemember to buckle up and drive safely");

}  

}

Explanation:

*The code is in Java.

Please see the comments in the code for explanation

4 0
3 years ago
Write a program that accepts a whole number as input, multiplies that number by 12, and then outputs the product
kenny6666 [7]

The program accepts a whole number as input, multiplies that number by 12, and then outputs the product

Explanation:

This program asks user to enter two integer numbers and displays the product.

The scanner class is used as input functions.

The code is shown below :  

import java.util.Scanner;

public class Demo {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.print("Enter number: ");

int num1 = scan.nextInt();

scan.close();

int product = num1*12;

System.out.println("Output: "+product);

   }

}

5 0
4 years ago
Read 2 more answers
When saving messages as drafts, it’s important to remember that your draft will not go live unless _______. Choose only ONE best
Nadusha1986 [10]

Answer:

Option (b) is the correct answer.

Explanation:

"Drafts" is a folder of messaging software or mail software whose works are to save the message until the user does not want to send it. So to send the message from drafts, the user needs to edit the message first then he can able to send that message. The above question asks that how a user can send the draft message. Option b suggests that a user needs to click send to send the draft message.

Hence it is a right option while the reason behind the other option is invalid are as follows--

  • Option a suggests that a user needs admin permission to send this but if that is his account then no need for admin permission.
  • Option c suggests to unlock the draft but the draft folder is not locked.
  • Option d suggests to take permission from a superuser but that is not required.
7 0
3 years ago
Programmers employ the acronym ____ to mean that if your input is incorrect, your output is worthless.
Nataly [62]
The answer is GIGO or the garbage in, garbage out. It is an idea regular to software engineering and science: the nature of yield is dictated by the nature of the information. In this way, for instance, if a scientific condition is disgracefully expressed, the appropriate response is probably not going to be right. It has a tendency to be utilized when discussing information section and databases. Essentially it implies that in the event that you enter off base or wrong information then you have truly entered waste.
8 0
3 years ago
Consider the following C program: int fun(int *i) { *i += 5; return 4; } 352 Chapter 7 Expressions and Assignment Statements voi
Musya8 [376]

Answer:

a. operands are evaluated left toright. 7

b. operands are evaluated right toleft. 12

Explanation:

a. operands are evaluated left toright. 7

b. operands are evaluated right toleft. 12

left to right

int fun (int *i) {

*i +=5; we add 5to x making x 8

return4; we return 4 tomain

}

void main ( ) {

int x = 3;

x = x + fun(&x); x starts at 3 in fun it ischanged to 8 but the 3 is already being used the 4 fromfun is added (3+4) to it making it 7

}

right toleft x starts at 3 in fun x gets changed to 8 and fun is returned as 4so we have 4 + 8 = 12

Paraphrasing

left to right it's is 3 + 4 =7 original value of x is used

right to left it is 4 + 8 =12 x is used as it was changed by fun, since fun was alreadyexecuted

4 0
3 years ago
Other questions:
  • A circuit has a resistance of 300 ohm and a current of 0.024 A. What is the voltage in the circuit?
    12·1 answer
  • The laser in a compact disc (cd) player uses light with a wavelength of 715 nm. what is the frequency of this light?
    9·1 answer
  • Keyshia now wants to remove the background of the image. She marks the areas she wants to keep and the areas she wants to remove
    13·2 answers
  • How to wire two separate switches &amp; lights using the same power source?
    14·1 answer
  • Why did Herman Hollerith invent the Tabulating Machine?
    10·1 answer
  • How do I do this? I know the shapes and everything but how do I do b, d, and e?
    7·1 answer
  • When the condition of an if statement is false, the computer will return an error message to the user.
    15·2 answers
  • Two time series techniques that are appropriate when the data display a strong upward or downward trend are ___________ and ____
    14·1 answer
  • Which of the following is not an example of Detailed Demographics?
    11·1 answer
  • What is the georgia connections academy administrative password? i want to install something.
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!