In this lesson I will cover how to use basic math operations, well addition really but you can also use subtraction[-], division[/] and multiplication[*].

I would also like to note that when doing division, if variabes of int type are used you will not get any remainders (i.e. n.345984). To be able to work with real numbers, numbers with decimals, you can use float. Below is a way to declare a float and assign it a value:

float floatNumber;

float Number = 28.47f;

Aside from generating random numbers and basic math operations, we will also incorporate an if statement and convert the user output to an integer. If you recall from the previous lesson, when a user enters an input, it is in string format. In order to perform math calculations correctly, we need to convert it to a numeric value (such as an integer in this instance).

Let's take a look at the code below. Be sure to look at the comments if something is unclear.

CODE


using System;

namespace lessonThree
{
    /// <summary>
    /// In this lesson we will cover some basic math
    /// operations, look at an if statement and a few other
    /// useful things that should be understood when
    /// programming in C#
    /// </summary>
    class thirdLesson
    {
        /// <summary>
        /// Ask an age.
        /// If under 18 -> Exit Program
        /// Give lucky number for the day
        /// Predict when person will get rich.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            int age;
            //Random object createRand
            Random createRand = new Random();
            int luckyNumber;

            Console.Write("Enter your age: ");
            //grab the input from user and convert it to
            //an integer, hence the 'Convert.ToInt32();
            age = Convert.ToInt32(Console.ReadLine());

            //skip line
            Console.WriteLine();
            
            if (age > 17)
            {
                
                //Random generated numbers range from 0 to 1,
                //therefore we multiply by ten (10) below
//The .NextDouble() method helps retrieve the value
                luckyNumber = (int)(createRand.NextDouble()*10);
                
                Console.WriteLine("Your Lucky Number is: {0}", luckyNumber);
                //Here you use the value of luckyNumber and add it to his age
                Console.WriteLine("You are {0} years old and will be rich at age: {1}", age, age+luckyNumber);
                //As you can see the zero in braces {0} holds the variable age
                //and the one in braces {1} holds the sum of the variables "age and luckyNumber" age+luckyNumber

            }//If the age is not 18 or above we conclude by saying
            
            Console.WriteLine("Thanks and Have a Great Day");
            Console.WriteLine();
            Console.WriteLine("Press [ENTER] to Exit");
            Console.ReadLine();

        }//end Main()
    }//end class
}//end namespace






As always, if you are testing this console application on Visual Studio or developing your own, stepping through the application with F11 is very useful.

 

 

 


Reply