0% found this document useful (0 votes)
8 views6 pages

Programming1 Week1 Exercises

The document provides instructions and code snippets for 5 exercises on learning basic C# programming concepts like input/output, data type conversion, displaying variables, arithmetic operators, and generating random numbers. The exercises cover using Visual Studio, Console applications, data types, operators, and input/output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Programming1 Week1 Exercises

The document provides instructions and code snippets for 5 exercises on learning basic C# programming concepts like input/output, data type conversion, displaying variables, arithmetic operators, and generating random numbers. The exercises cover using Visual Studio, Console applications, data types, operators, and input/output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming 1 – Week 1 - Exercises

Exercise 0 - Visual studio 2022 and C#


1. Open Visual Studio 2022

2. Click in the menubar on:


a. File
b. New
c. Project…

3.
a b c
a. Choose Language: C#
b. Choose Platform: Windows
c. Choose Project type: Console
d. Select Console App
d
e. Click button Next

4.
a. Enter as Project name: ‘assignment1’.
b. Choose a suitable location for your project
files a
c. Enter as Solution name:
‘IntroductionProgramming1’
d. Click button Next
b

5.
a. Select framework: .NET 6.0
b. Check ‘Do not use top-level statements’
c. Click button Create

(September 2022)
Programming 1 – Week 1 - Exercises

Exercise 1 - input/output
In this exercise we will create a simple program that has some input and output.

1. Enter the code as shown. 2

2. Click at the top on the Start-button to run the


program.

3. What happens if you change the last line to


‘Console.ReadLine()’?

4. What happens if you change each of the


‘Console.WriteLine(…)’ in ‘Console.Write(…)’?

5. Adjust the program to make it more user


friendly, by adding the text “Enter your name:”
and “Enter your age: ” prior to when the user
has to enter them. Use ‘Console.Write(…)’ or
‘Console.WriteLine(…)’.
4

(September 2022)
Programming 1 – Week 1 - Exercises

Exercise 2 - conversion
In this exercise we take a look at converting the data type of one variable into another data type, also known as a
conversion.

1.
a. Right-click on Solution
‘IntroductionProgramming1’
b. Click on Add
c. Click on New Project…

2.
a. Choose again ‘Console App’
b. Click on button Next

3.
a. Enter as Project name: assignment2
b. Click on button Next

4.
a. Select framework: .NET 6.0
b. Check ‘Do not use top-level
statements’
c. Click button Create

(September 2022)
Programming 1 – Week 1 - Exercises

The new project will not do anything yet, since assignment1 is still the ‘startup project’. We will have to change this.

5.
a. Right-click on assignment2
b. Click on Set as StartUp Project

To convert a string to an int, we use int.Parse(variable).


To convert an int to a string, we use variable.ToString().

1. Enter the code shown to the right.

2. Convert the string value (input) to an int-value


(age) where it has ??? [2]

3. Convert the int value (age) to a string value


(output) where it has ??? [3] 2

4. Run the program and check if it works.

5. Replace the variable output with age at the 3


Console.WriteLine(…). Does this still work? 5

6. To increase the age with 1 you can also use


+=1, or age++. Test both.

(September 2022)
Programming 1 – Week 1 - Exercises

Exercise 3 – displaying variables


In this exercise we take a look at several ways to display a variable (on screen).

1. Add a new project to solution


IntroductionProgramming1, again a Console
App (.NET Core), and use as project name
‘assignment3’.

2. Enter the code shown to the right.

3. Look carefully at the 3 different ways to


display a variable. The 3rd option (string
interpolation) provides a more readable and
convenient syntax to create formatted strings.
Note the $-sign at the start of the text!

4. There’s a bug in the program. Fix it.

(September 2022)
Programming 1 – Week 1 - Exercises

Exercise 4 – mathematical operators


In this exercise we will apply some mathematical (arithmetic) operators on a variable.

1. Add a new project to solution


IntroductionProgramming1, again as Console
App, and use as project name ‘assignment4’.

2. Enter the code shown to the right.

3. Complete the code where it has ??? [3]

4. Complete the where is has ??? [4]

5. Insert a breakpoint (with F9) on the line


where variable ‘number’ is declared (and
filled) and run the program step by step (with 3
F10); after each step check the value of
variable ‘result’. You can check the value by 4
hovering the mouse-pointer above the
variable, or by inspecting the variable in the
‘Autos’ screen below.

Exercise 5 – Mathematics
On the exam of Mathematics one of the first questions is often to add four positive integers between 100 and 10000. In
C# you can generate a random number using:
Random rnd = new Random();
And then declare a random variable using for example
int number1 = rnd.Next(101, 10000);

• Write a program that will show four numbers between 101 and 10000, then asks to add these up, and checks
the result.

(September 2022)

You might also like