0% found this document useful (0 votes)
553 views

C# Practice Exercises 1

1. The document provides 4 practice exercises involving simple C# programs using arrays, functions, and user input. The first asks the user to write a program that declares and sums an array of 5 double values. The second asks the user to declare two arrays, one for 10 integers and another for their squares, and calculate the squares without hardcoding. The third asks the user to write a program to solve a quadratic equation for coefficients entered by the user, using functions. The fourth asks the user to write multiple functions - one to get integer input, one each for multiplication and division, and one to call these and perform a calculation.

Uploaded by

Tony Nakovski
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
553 views

C# Practice Exercises 1

1. The document provides 4 practice exercises involving simple C# programs using arrays, functions, and user input. The first asks the user to write a program that declares and sums an array of 5 double values. The second asks the user to declare two arrays, one for 10 integers and another for their squares, and calculate the squares without hardcoding. The third asks the user to write a program to solve a quadratic equation for coefficients entered by the user, using functions. The fourth asks the user to write multiple functions - one to get integer input, one each for multiplication and division, and one to call these and perform a calculation.

Uploaded by

Tony Nakovski
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practice Exercises Simple Programs, Functions, and Arrays 1.

. Write a program that declares an array of five double values and then sums the values. Print the sum on screen.

2. Write a program that declares two arrays: one in which you will store 10 integers and another one that you will use for storing the squared values of the 10 integers (so, if your array is called numbers and numbers[0] stores the number 10, then the other array, that might be called squares, will store 100 in squares[0]). Dont, however, hardcode the squares, but calculate them using *, which is the multiplication operator.

3. For taking user input in C#, we are using the function Console.ReadLine(); This function reads what the user enters (until Enter is pressed) and stores the input as a string of characters. If we want to take an int or a double value from the user, we have to convert the input like in the following examples: int.parse(Console.ReadLine()); double.parse(Console.ReadLine()); (Note: entering alphabetic or special characters instead of numbers will cause a failure in the conversion; well see how to address that problem later in the course). Knowing this, solve the last problem that you had on the quiz in C#: Imagine that we want to solve the quadratic equation

for coefficients a, b, and c entered by a user. Write a C# program solving the equation (first ask the user to enter the coefficients, then calculate the result, and print the result on screen). For calculating square root in C#, use the Math.Sqrt() function which takes a parameter of type double. Hint: for calculating the result, use a function which takes the coefficients as parameters and returns only one of the solutions of the equation.

Practice Exercises Simple Programs, Functions, and Arrays 4. In this problem, you will practice using multiple functions in a program. Write one function called TakeInput that asks a user to enter an integer and returns the integer. Then, write another function called Calculate that performs the following calculation (a * a) + (a / a) where a is the integer entered. Dont calculate (a * a) and (a / a) in one line of code, but use one function for multiplication, called Multiply, and one for division, called Divide; hence, Calculate should call both Multiply and Divide. Note that if a is 0, the program will fail, but we are ignoring that in this example.

You might also like