Variables often hold text or numeric data. In C# there are several types of variables,
including strings and numeric variables.

Variables can be reused in your code. Arithmetic operations can be used on numeric variables.
String variables can also be changed (sub-strings, concatenation).

Variables in C#

Numeric variables

Lets start with numeric variables. We create a program that calculates
the VAT for a given price.

Define a series of products, sum the price ex. VAT, then calculate the VAT and add it to the price.
Copy the code below and save the file as variables.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;

namespace ExampleApplication {

class ExampleProg {

static void Main(string[] args) {
float price = 2 + 3;
float vat = price * 0.15;
float total = price + vat;

Console.WriteLine(total);
Console.ReadKey() ;
}
}
}

All arithmetic operations can be run on variables: division (/), substraction (-), addition (+) and multiplication (*)

Exercises

  1. Calculate the year given the date of birth and age
  2. Create a program that calculates the average weight of 5 people.