First C# Program

Author: Ronald S. Holland
at
Total Application Works






To visit my site

HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#


To contact us

© 2002 - 2016 All Rights Reserved Total Application Works


  • Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)

  • <html>
    <head>
    <title>Example of a link </title>
    </head>
    <body>
    <a href="http://sumtotalz.com/TotalAppsWorks/CSharp/FirstCSharpProgram_Tut.html">
    VS 2008 Getting Started with Visual C# </a>
    </table>
    </body>
    </html>
    Source and see a screen shot


Read Me First

The FCSharpPgm.zip compressed file contains the following seven projects:

  1. helloworld.cs
  2. helloworld.MS VS Solution (Microsoft Visual studio SolutionJ
  3. secondPgm.cs
  4. secondPgm.MS VS Solution
  5. thirdPgm.cs
  6. thirdPgm.MS VS Solution
  7. fourthPgm.cs
  8. fourthPgm.MS VS Solution
  9. fifthPgm.cs
  10. fifthPgm.MS VS Solution
  11. sixthPgm.cs
  12. sixthPgm.MS VS Solution
  13. seventhPgm.cs
  14. seventhPgm.MS VS Solution
  15. FCSharpPgm.zip
  16. FirstCSharpProgram_Tut.html
To run the project:
  1. Save
  2. Go to the folder where you downloaded the FCSharpPgm.zip compressed file
  3. Extract the files
  4. Right click on one of the ****.MS VS Solution - click on the version of Microsoft studio you have
  5. When Microsoft studio IDE is started
  6. Click on the Debug menu
  7. Click on the Start without Debugging menu item
    • The ****.MS VS Solution will begin to execute
  8. Copy FCSharpPgm.zip to another folder called template for future use.

Introduction

This tutorial is intended for that person who is getting started with C#. In order for you to be able to follow this tutorial, you need a C++/C# compiler. I don't believe in buying something if you can get it for free. At this point, if you don't have a compiler, you can download one for free from

Some tutorials on Microsoft's Visual Studio C++/C# 2008 Edition


There are some books on the market that come with a CD containing a compiler e.g., Sam's Teach Yourself C# in 21 Days. This is not an endorsement of the book, but simply a suggestion of how to get a top quality C++ compiler. If you buy a book, make sure it contains a copy of a C# Compiler or preferably a copy of the MS Visual Studio C++. First year college textbooks on C++ may also be a good source. Before you spend any money, download Microsoft's Visual Studio C++ 2008 Edition free copy.

What is a Template?

If you look in a dictionary, template is defined as

    a pattern, usually in the form of a thin plate of metal, wood, plastic, etc., for forming an accurate copy of an object or shape

Our template looks like:

  
/***************************************************
 * Comments
 * 
 **************************************************/

// Namespace Declaration
using System;

class template  {
	/** *********************************************
         *  The main() method is the entry poin that is 
         *  called when this program is loaded ino memory.
         ************************************************/  
   static void Main()  {

   } 

} 
  

In the above template,
  • { starts the beginning of a class and method (where the code resides), and
  • } indicates the end of the method and class
  • static void main() method is the entry point that is called when the program is loaded into memory.
You should compile and execute the above template.

Writing Our First Program

We will use the above template to write our first program.

  1. We will change the multiline comment to look like:
      
    /***************************************************
     * File: helloworld.cs
     *
     * Purpose: Write a message to the standard out
     * 
     **************************************************/
      
  2. We will change the main() method to
    Source
      
    static void main()  {
        Console.WriteLine(" Hello World!\n\n" ) ;
        cConsole.WriteLine(" from\n\n"  ) ;
        cConsole.WriteLine(" Ronald S. Holland\n\n"  ) ;
        Console.WriteLine("at\n\n"  ) ;
        Console.WriteLine(" Total Application Works\n\n\"  ) ;
    }
      

    Figure 1: Hello World
  3. When we put it all together, we get
    Source
      
    /*****************************************************
     * File: helloworld.cs
     *
     * Purpose: Write a message to the standard out
     ****************************************************/
    
    // Namespace Declaration
    using System;
    
    class helloWorld {
    
       static void Main()  {
          Console.WriteLine(" Hello World!\n\n" ) ;
          Console.WriteLine(" from\n\n"  ) ;
          Console.WriteLine(" Ronald S. Holland\n\n"  ) ;
          Console.WriteLine("at\n\n"  ) ;
          Console.WriteLine(" Total Application Works\n\n"  ) ;
    
          Console.ReadLine() ;
       }
    }
      

    Output
     Hello World!
    
    
     from
    
    
     Ronald S. Holland
    
    
     at
    
    
     Total Application Works
    
    
    Bat File
    rem You may have to right-click on helloworld.exe 
    rem    to delete this file 
    
    Del  helloworld.exe
    
    csc.exe helloworld.cs
    
    helloworld.exe
    
    Figure 2: Hello World
    In the above code,
    • Console.WriteLine - The object writes to the standard output e.g., screen
    • To create source and Bat files:
      1. Open up a new Notepad file
      2. Copy the helloworld.cs source code from figure 6 into the new Notepad file
      3. Name the file helloworld.cs
      4. Save helloworld.cs
      5. Open up a new Notepad file
      6. Copy the bat file contents from figure 6 into the new Notepad file
      7. Name the file helloworld.bat
      8. Save helloworld.bat
      9. Double-click helloworld.bat
      10. You may have to right-click on helloworld.exe to delete this file
      11. Double-click on helloworld application

Writing Our Second Program

Our Second Program will

  • Declare two integer variables a and b.
  • Initialize a to 5 and b to 10.
  • Add a and b together.
  • Write the result to the screen.
Source
  
/*****************************************************
 * File: secondPgm.cs
 *
 * Purpose: add two number and write to the standard out
 ****************************************************/

// Namespace Declaration
using System;

class secondPgm  {
   static void Main()  {
      int a, b;

      a = 5 ;
      b = 10 ; 

      Console.WriteLine("The value of a is " + a  + "\n"  + "\n" ) ;
      Console.WriteLine("The value of b is " + b + "\n"  + "\n" ) ;

      Console.WriteLine("The value of a + b is " + (a + b) + "\n\n\n" ) ;

      Console.ReadLine() ;
   } 
} 
  
  

Output
The value of a is 5

The value of b is 10

The value of a + b is 15
Figure 3: Second program source and output
  1. Open up a new Notepad file
  2. Copy the secondPgm.cs source code from Figure 3 into the new Notepad file
  3. Name the file secondPgm.cs
  4. Save secondPgm.cs
  5. Open up a new Notepad file
  6. Copy the bat file contents from Figure 3 into the new Notepad file
  7. Name the file secondPgm.bat
  8. Save secondPgm.bat
  9. Double-click secondPgm.bat
  10. You may have to right-click on secondPgm.exe to delete this file
  11. Double-click on secondPgm.exe application

Writing Our Third Program

Our Third Program will

  • Declare three integer variables a, b and c.
  • Initialize a to 5 and b to 10.
  • Add a and b together and store the result in c.
  • Write the value of c to the screen.
Source Code
  
/**************************************************************
 * File: thirdPgm.cs
 *
 * Purpose: 
 *      1- Get two numbers from the user;
 *      2- Add these two numbers; 
 *      3- store the result in variable , and; 
 *      4- Then write the value of c out to the screen.
 *           
 **************************************************************/

// Namespace Declaration
using System;


class thirdPgm  {
	// Main begins program execution.
   static void Main()  {
      int a, b, c;

      Console.WriteLine("Enter the first integer" + "\n");
      a = int.Parse(Console.ReadLine());

      Console.WriteLine("Enter the second integer" + "\n");
      b = int.Parse(Console.ReadLine());


      c = a + b ;

      Console.WriteLine( "The value of a is " + a + "\n" ) ;
      Console.WriteLine( "The value of b is " + b + "\n" ) ;
      Console.WriteLine( "The value of a + b is "  +  c  +  "\n\n\n"  ) ; 

		// Make window wait for input
      Console.ReadLine();
   }
}
  

Output
 
The value of a is 5

The value of b is 10

The value of a + b is 15
 
Figure 4: Third program example

Writing Our Fourth Program

Instead of initializing the values for a and b, our Fourth Program will request the values from the user.

  • Declare three integer variables a, b and sum.
  • Request that the user enter two integers, storing one integer in a and the other in b.
  • Add a and b together and store the result in sum.
  • Write the value of sum to the screen.
Source Code
  
/*****************************************************
 * File: fourthPgm.cs
 *
 * Purpose: request two numbers from the user; 
 * 1- Add the two numbers and store the result in 
 *     variable sum
 * 2- Write the value of sum out to the screen
 * 3- Multiply the two numbers and store the result 
 *     in variable product
 * 4- Write the value of product out to the screen
 * 5- Add the two numbers, compute the average and 
 *     store the result in variable average
 * 6- Write the value of average out to the screen
 *           
 *****************************************************/


// Namespace Declaration
using System;

class fourthPgm {

   static void Main()  { 
      int a, b, sum, product;
      double average = 0.0;

      Console.WriteLine("Enter the first integer"  + "\n" ) ;
      a = int.Parse( Console.ReadLine() ) ;

      Console.WriteLine("Enter the second integer"  + "\n" ) ;
      b = int.Parse( Console.ReadLine()  ) ;

      sum     = a + b ;
      product = a * b ;
      average = (double)(a + b) / 2 ;   // cast int to double


      Console.WriteLine("The value of a is " + a  + "\n" ) ;
      Console.WriteLine("The value of b is " +  b  + "\n" ) ;
      Console.WriteLine("The value of sum = a + b is " + sum  + "\n" ) ;
      Console.WriteLine("The value of product = a * b is " + product  + "\n" ) ;
      Console.WriteLine("The value of average = (a + b) / 2 is " + average  + "\n" ) ;

      Console.ReadLine() ;
   }
  
Output
 
Enter the first integer
5
Enter the second integer
5
The value of a is 5
The value of b is 5
The value of sum = a + b is 10
The value of product = a * b is 25
The value of average = (a + b) / 2 is 5
Press any key to continue . . .
 
Figure 5: Fourth Program Example


In the above example, Console.WriteLine writes output to the screen and Console.ReadLine() controls extractions/reads from the standard input (keyboard).

Writing Our Fifth Program

In our fifth program, we will

  • Declare four integer variables a, b, c, sum, product, and average.
  • Request that the user enter three integers, storing one integer in a, one integer in b and the third in c.
  • Add a, b and c together and store the result in sum.
      Write the value of sum to the screen
  • Multiply a, b and c together and store the result in product.
      Write the value of product to the screen
  • Add a, b and c together, calculate the average and store the result in average.
      Write the value of sum to the screen
    Source Code
      
    /*****************************************************
     * File: fifthPgm.cs
     *
     * Purpose: request three numbers from the user. 
     * 1- Add the three numbers and store the result in 
     *     variable sum
     * 2- Write the value of sum to the screen 
     * 3- Multiply the three numbers together and store 
     *     the result in variable product
     * 4- Write the value of product to the screen 
     * 5- Add the three numbers and compute the average 
     *     and then store the value in the variable 
     *     average
     *           
     *****************************************************/
    
    // Namespace Declaration
    using System;
    
    class fifthPgm  {
    	// Main begins program execution.
       static void Main()  {
    
           int a, b, c, sum, product;
           double average;
    
          Console.WriteLine(  "Enter the first integer" + "\n" ) ;
          a = int.Parse( Console.ReadLine() );
    
          Console.WriteLine( "Enter the second integer" + "\n" ) ;
          b = int.Parse( Console.ReadLine()  );
    
          Console.WriteLine( "Enter the third integer" + "\n" ) ;
          c = int.Parse( Console.ReadLine()  );
    
          sum      = a + b + c ;
          product  = a * b * c ;
          average = (double)(a + b + c) / 3 ;  //  Cast int. to double
    
          Console.WriteLine( "The value of a is "  + a  + "\n" ) ;
          Console.WriteLine( "The value of b is "  +  b  + "\n" ) ;
          Console.WriteLine( "The value of c is "  +  c  + "\n" ) ;
          Console.WriteLine( "The value of a + b + c is " + sum  + "\n" ) ;
          Console.WriteLine( "The value of a * b * c is "  + product  + "\n" ) ;
          Console.WriteLine( "The value of (a + b + c) / 3 is "  + average  + "\n" ) ;
    
          Console.ReadLine() ;
    
       } 
    
    
      
    Output
     
    Enter the first integer
    
    5
    Enter the second integer
    
    5
    Enter the third integer
    
    5
    The value of a is 5
    
    The value of b is 5
    
    The value of c is 5
    
    The value of a + b + c is 15
    
    The value of a * b * c is 125
    
    The value of (a + b + c) / 3 is 5
    
     
    Bat. file used to compile Fifth Program Example
     
    Del  fifthPgm.exe
    
    csc.exe fifthPgm.cs
    
    Figure 6: Fifth Program Example


    1. Open up a new Notepad file
    2. Copy the fifthPgm.cs source code from figure 6 into the new Notepad file
    3. Name the file fifthPgm.cs
    4. Save fifthPgm.cs
    5. Open up a new Notepad file
    6. Copy the bat file contents from figure 6 into the new Notepad file
    7. Name the file fifthPgm.bat
    8. Save fifthPgm.bat
    9. Double-click fifthPgm.bat
    10. You may have to right-click on fifthPgm.exe to delete this file
    11. Double-click on fifthPgm application

Writing Our Sixth Program

Our Sixth Program will request two numbers from the user and raise the first value to the power of the second value.

  • Declare four integer variables x, y, ii and power.
  • Request that the user enter two integers, storing one integer in x and the other in y.
  • Multiply x and y times and store the result in power.
  • Write the value of power to the screen.
Source Code
  
/*****************************************************
 * File: sixthPgm.cs
 *
 * Purpose: Obtain two numbers. raise the first number 
 *          to the power of the second e.g., x ^ y
 *          - raise x to the y power 
 ****************************************************/

// Namespace Declaration
using System;

class sixthPgm {
	// Main begins program execution.
   static void Main()  {
   int x, y, ii, power;

   ii = 1;
   power = 1;
   Console.WriteLine("Enter the first integer" + "\n" ) ;
   x = int.Parse( Console.ReadLine() );

   Console.WriteLine("Enter the second integer" + "\n" ) ;
   y = int.Parse( Console.ReadLine() );

   while (ii <= y) {
      power *= x;
      ++ii;
   }

   Console.WriteLine("The value of " + x + "^" + y + 
                       " is " + power + "\n\n\n"  ) ;
   
}
  
Output
 
Enter the first integer
5
Enter the second integer
3
The value of 5^3 is 125

 
Figure 7: Sixth Program Example


Writing Our Seventh Program

Our Seventh Program will request two numbers from the user and raise the first value to the power of the second value. Requirements for our program are:

  • Declare one integer variable workDays.
  • Declare three float variables workHours, payRate, weeklyPay.
  • Multiply the workDays by workHours by payRate to get weeklyPay.
  • Write the value of workDays, workHours, and weeklyPay to the screen.
  • After implementing the above requirements, the results look like:

    Source Code
      
    /*****************************************************
     * File: seventhPgm.cs
     *
     * Purpose: Calculate the weekly pay of a worker.
     * 1- Declare the variables  that will be used.
     * 2- Ask the user how many days worked this week.
     * 3- Ask the user how many hours worked this week.
     * 4- Compute the average hours worked per day.
     * 5- Ask the user for their payrate.
     * 6- Compute the weekly salary.
     * 7- Print to screen the 
     *    - workDays
     *    - workHours
     *    - weeklyPay
     *
     ****************************************************/
    
    // Namespace Declaration
    using System;
    
    class seventhPgm {
    
    	// Main begins program execution.
       static void Main()  {
          int	    workDays  = 0;
          float    workHours = 0, payRate = 0, weeklyPay = 0 ;
    
          Console.WriteLine("How many days did you work this week? " + "\n" ) ;
          workDays = int.Parse( Console.ReadLine() );
    
          Console.WriteLine("How many hours did you work this week? " + "\n" ) ;
          workHours = float.Parse( Console.ReadLine() );
                // average hours per day
          workHours = workHours / workDays ; // average hours per day
    
          Console.WriteLine("What is your payrate? " + "\n" ) ;
          payRate = float.Parse( Console.ReadLine() );
    
    
          weeklyPay = (float)workDays * workHours * payRate;
    
          Console.WriteLine("workDays =s " + workDays + "\n" ) ;
          Console.WriteLine("Average workHours per day =s " + workHours + "\n\n" ) ;
          Console.WriteLine("Weekly Pay =s " + weeklyPay + "\n\n" ) ;
    
          Console.ReadLine() ;
       }
    }
    
    
      
    Output
     
    How many days did you work this week?
    
    5
    How many hours did you work this week?
    
    55
    What is your payrate?
    
    39.5
    workDays =s 5
    
    Average workHours per day =s 11
    
    
    Weekly Pay =s 2172.5
    
    
    
     
    Figure 7: Seventh Program Example


Definitions

Comment - comments help document a program but do cause the computer to perform any action. They are ignored when the program executes. They are of the form:

  • // this symbol indicates that all that follows the // is a comment
  • A multiline comment starts with /* and ends with */
    /*
     * This is a multiline comment
     */
    

Function - a named section of a program that performs the specific task included between the { and the }.

<iostream> - to declare objects that control reading from and writing to the standard streams. This is often the only header you need include to perform input and output from a C++ program.

Solution to #5

Source Code
  
  
  
/*****************************************************
 * File: fifthPgm.cpp
 * Purpose: request three numbers from the user,  

 * 1- Add the three numbers and store the result in 
 *    variable sum
 * 2- Multiply the three numbers and store the result 
 *    in variable product
 * 3- add the three numbers, determine the average of 
 *    the three numbers and store the result in 
 *    variable average
 *
 * 4- Write the three values entered by the user
 * 5- Write the value of sum out to the screen
 * 6- Write the value of product out to the screen 
 * 7- Write the value of average out to the screen 
 *           
 *****************************************************/

#include <iostream>

void main()  {
   int a, b, c, sum, product, average;

   cout << "Enter the first integer" << endl;
   cin >> a ;

   cout << "Enter the second integer" << endl;
   cin >> b ;

   cout << "Enter the third integer" << endl;
   cin >> c ;

   sum      = a + b + c ;
   product  = a * b * c ;
   average = (a + b + c) / 3 ;

   cout << "The value of a is " << a << endl;
   cout << "The value of b is " <<  b << endl;
   cout << "The value of c is " <<  c << endl;
   cout << "The value of a + b + c is " << sum << endl;
   cout << "The value of a * b * c is " << product << endl;
   cout << "The value of (a + b + c) / 3 is " << average << endl;
} 
  
  
Output
 
Enter the first integer
5
Enter the second integer
5
Enter the third integer
5
The value of a is 5
The value of b is 5
The value of c is 5
The value of a + b + c is 15
The value of a * b * c is 125
The value of (a + b + c) / 3 is 5
Press any key to continue . . .
 
Figure 8: Fifth Program Example

Installing Microsoft's Visual Studio C++ 2008 Edition

This tool is not supported by Microsoft product support services. Below you will find the Readme instructions that comes with the Virtual CD-ROM Control Panel v2.0.1.1 download.

 
  1. System Requirements
    • Windows XP Home or Windows XP Professional
    • I copied the XP copy of Microsoft's Visual Studio C++ 2008 Edition onto the Vista operating system
      • Windows Explorer doesn't see the virtual drive.
  2. Installation instructions
    1. Copy VCdRom.sys to your WINDOWS\system32\drivers folder.
    2. Execute VCdControlTool.exe



    3. Click Driver control



    4. If the "Install Driver button is available, click it. Navigate to the WINDOWS\system32\drivers folder, select VCdRom.sys, and click Open.
    5. Click Start
    6. Click OK.
    7. Click Add Drive to add a drive to the drive list. Ensure that the drive added is not a local drive. If it is, continue to click Add Drive until an unused drive letter is available.



    8. Select an unused drive letter from the drive list and click Mount.
    9. Navigate to the image file, select it, and press OK. UNC naming conventions should not be used, however mapped network drives should be OK. Click OK.



    At this point, you may can use the drive letter as if it were a local CD-ROM device. When you are finished you may
    1. unmount,
    2. stop, and
    3. remove the driver from memory using the driver control dialog.






HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript C#

© 2002 - 2016 All Rights Reserved Total Application Works