Tuesday, October 6, 2015

Ints doubles and operators

Here is the code from today's class.

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

namespace ExamplesForAssignment2
{
    class Program
    {
        /*************************
        These are examples for assignment 2
        involving operators 
        +   addition
        -    subtraction
        *    multiplication
        /    division
        %   modulus--remainder in integer division
        and the numeric data types
        int, whole number
        and double, float or decimal 
        ****************************/

        static void Main(string[] args)
        {
            //number variables int double
            int number, number2;
            int sum, difference, product, quotient, remainder;
            double  exponent;
            double decimalQuotient;

            //inputs
            Console.WriteLine("enter an integer");
            //C# treats all input from the console as a string
            //Parse removes the quotes and sees if the content
            //is of the correct type--in this case int
            number = int.Parse(Console.ReadLine());

            Console.WriteLine("enter another integer");
            number2 = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter a double");
            //this is an example of parsing a double from the console
            double newNumber = double.Parse(Console.ReadLine());

            //algorithm
            //Here are all the operators
            sum = number + number2;
            difference = number - number2;
            product = number * number2;
            quotient = number / number2;
            remainder = number % number2;
            //in this one we cast one of the sides to a double
            //the equation always defaults to the type with the higher precision
            //doubles always have a higher precision because they contain doubles
            //this makes it so the result returns the decimal part
            decimalQuotient = (double)number / number2;
            //the Math library is static and always available
            exponent = Math.Pow(number, number2);

            //outputs
            Console.WriteLine("The sum of {0}, and {1} is {2}", number, number2, sum);
            Console.WriteLine("The difference of {0}, and {1} is {2}", number, number2, difference);
            Console.WriteLine("The product of {0}, and {1} is {2}", number, number2, product);
            Console.WriteLine("The quotient of {0}, and {1} is {2}", number, number2, quotient);
            Console.WriteLine("The remainder of {0}, and {1} is {2}", number, number2, remainder);
            Console.WriteLine("The exponent of {0}, and {1} is {2}",number, number2, exponent);
            Console.WriteLine("The decimal quotient of {0}, and {1} is {2}", number, number2, decimalQuotient);

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();



        }
    }
}

No comments:

Post a Comment