Monday, September 29, 2014

Basic numbers and operators

Here is what we did in class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NumberExample
{
    class Program
    {
        /* ************************
         * this program is just an example of using
         * numeric types, specifically int and double
         * and the basic math operators
         * steve conger 9/29/2014 Evening class
         * **********************/
        static void Main(string[] args)
        {
            //declaring an int. You can declare several
            //variables at once as long as they are the same
            //type.
            int number, numberb, numberc;
            double number2; //a double has decimal places
            const double PI =3.14; //a constant can't be changed
            //constants are all caps by convention.

            Console.WriteLine("Enter an Integer");
            //everything entered on the console is a string
            //(true also of textboxes) and must be Parsed
            //or converted to the numeric type--in this case int
            number = int.Parse(Console.ReadLine());
            //another way to convert string to int
            //number = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter another integer");
            numberb = int.Parse(Console.ReadLine());
            
            /**************************
             * operators
             * ***********************/

            //addition
            numberc = number + numberb;
            Console.WriteLine("{0} + {1} = {2}", number, numberb, numberc);

            //subtraction
            numberc = number - numberb;
            Console.WriteLine("{0} - {1} = {2}", number, numberb, numberc);

            //multiplication
            numberc = number * numberb;
            Console.WriteLine("{0} * {1} = {2}", number, numberb, numberc);

            //Integer division. In integer division only a whole number is returned 
            //5/2=2 Any remainder or decimal part is dropped
            numberc = number / numberb;
            Console.WriteLine("{0} / {1} = {2}", number, numberb, numberc);

            //Modulus
            //for integers the modulus % returns the remainder in an integer
            //division 5%2=1 because 5/2 = 2 with a remainder of 1
            numberc = number % numberb;
            Console.WriteLine("{0} % {1} = {2}", number, numberb, numberc);

            //doubles need to be parsed as a double
            Console.WriteLine("Enter a double");
            number2 = double.Parse(Console.ReadLine());


            //this will still result in 5/2=2 becuase the division is still between two
            //integers
            number2 = number / numberb;
            Console.WriteLine("{0} % {1} = {2}", number, numberb, number2);

            //if you want the result to be a double you have to "Cast" one side of
            //the division to a double. (double)number does that.
            number2 = (double)number / numberb;
            Console.WriteLine("{0} % {1} = {2}", number, numberb, number2);

            //The Math library is a static library that is always available.
            //just type Math and a dot to see the available
            Console.WriteLine(Math.Sqrt(number2));

            //Pause for Visual Studio
            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment