Write a simple math program in C# and compile using C# compiler Windows 8

If you want to write a simple C# program or utility with 50-100 lines of code, for example, a program that printing “Hello World” on screen, set the date of a file or simple math quiz program (like the one below) you do not need to download any version of Visual Studio.

This article teaches you how to compile C# program using the C# compiler that comes with Windows 8. In order to demo this I have return a simple math quiz program.

You can copy the code below to compile in your Windows 8.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace ConAppMathWin8

{

  classProgram

  {

    staticvoid Main(string[] args)

    {

      //Declare for the ramdon number

      byte[] bytes1 = newbyte[50];

     

      int result = 0;

      int choose = 0;

      float answer = 0;

      float[] ansCollection = newfloat[5];

      Random rnd1 = newRandom();

      //Init the random number

      rnd1.NextBytes(bytes1);

      //Selct a choose

        Console.WriteLine(“========Play math========”);

        Console.WriteLine(“1. Add”);

        Console.WriteLine(“2. Subtract”);

        Console.WriteLine(“3. Multiply”);

        Console.WriteLine(“4. Divide”);

        Console.Write(“Choose: “);

        //Get the user input for choose

        try

        {

          choose = int.Parse(Console.ReadLine());

        }

        catch (Exception ex)

        {

          Console.WriteLine(“Error: {0}”, ex.ToString());

        }

 

      for (int i = 0; i < 5; i++)

      {

        switch(choose)

        {

          case 1:

            Console.Write(“What is the answer of {0} + {1} = “, bytes1[i], bytes1[i + 5]);

            //Storing result into array

            ansCollection[i] = Add(bytes1[i], bytes1[i + 5]);

            break;

          case 2:

            Console.Write(“What is the answer of {0} – {1} = “, bytes1[i], bytes1[i + 5]);

            //Storing result into array

            ansCollection[i] = Subtract(bytes1[i], bytes1[i + 5]);

            break;

          case 3:

            Console.Write(“What is the answer of {0} x {1} = “, bytes1[i], bytes1[i + 5]);

            //Storing result into array

            ansCollection[i] = Multiply(bytes1[i], bytes1[i + 5]);

            break;

          case 4:

            Console.Write(“What is the answer of {0} / {1} = “, bytes1[i], bytes1[i + 5]);

            //Storing result into array

            ansCollection[i] = Divide(bytes1[i], bytes1[i + 5]);

            break;

        }

        //Get input from player

        try

        {

        answer = float.Parse(Console.ReadLine());

        }

        catch (Exception ex)

        {

          Console.WriteLine(“Error: {0}”, ex.ToString());

        }

        //Add to counter if the answer is correct

        if (answer == ansCollection[i])

          result++;

      }

 

      Console.WriteLine(“******** Answer ************”);

      for (int i = 0; i < 5; i++)

      {

        //write answer

        Console.WriteLine(“Answer {0}: {1}”, i, ansCollection[i]);

      }

      Console.WriteLine(“%%%%%%%%%%%%%%%%%%%%”);

      //Write total correct answer(s)

      Console.WriteLine(“You got {0} correct!”, result);

      Console.ReadLine();

    }

 

    //Add 2 numbers

    publicstaticint Add(int i1, int i2)

    {

      return (i1+i2);

    }

    //Subtract 2 numbers

    publicstaticint Subtract(int i1, int i2)

    {

      return (i1-i2);

    }

    //Multiply 2 numbers

    publicstaticint Multiply(int i1, int i2)

    {

      return (i1*i2);

    }

    //Divide 2 numbers

    publicstaticfloat Divide(int i1, int i2)

    {

      return (i1/i2);

    }

  }

}

 

1. Copy the code above (if you do not have your own C# code).

2. Paste the above code to notepad and save as math.cs (if you copy the code).

3. Press Windows key and type cmd.

clip_image002

4. Go to the folder that you safe your file with cs extension, in our case math.cs.

5. Type c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc math.cs.

csc is the C# compiler.

6. You will find math.exe is generated.

7. Type the program name, math.exe, then the program will run.

Advertisements

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s