CSharp - Write program to Do Calculations with Variables


Use several variables at once.

You are going to store some numbers in two variables.

After that, you will calculate their sum into the third one.


You can declare a variable and immediately store a value in it.

You can also declare a variable first and store a value in it later.


using System;

class Program{/*www.j  a  v a  2s  .c  o  m*/
    static void Main(string[] args){
        // Values to be summed 
        int firstNumber = 42;
        int secondNumber = 11;

        // Calculating 
        int sum = firstNumber + secondNumber;

        // Output 
        Console.WriteLine("Sum is: " + sum);

        // Declaring all variables at once 
        int thirdNumber, fourthNumber, newSum;

        // Values to be summed 
        thirdNumber = 42;
        fourthNumber = 11;

        // Calculating 
        newSum = thirdNumber + fourthNumber;

        // Output 
        Console.WriteLine("Calculated another way: " + newSum);