C Sharp Console Application and sample program of Addition of two integers

Today we are gonna introduced to the basic C Sharp Console Application. We will be using Visual Studio IDE for learning and programming C Sharp. Visual studio is the best IDE for .Net Programs. You can download free trial or purchase it from Microsoft Official Site.

Console Application C# Console Application is the application which takes input and displays output on Command Line interface. It is the simplest application which is having no user interface and with very minimum functionalities.Console Application is having three functional Data Streams , Standard Input, Standard Output and Standard Error Follow the given steps to create your first Console Application.



  • Download and Open the Visual Studio(Any version. I am using Visual studio ultimate 2012).
  • Click on the new Project
  • A Window will appear giving you all types of templates and project you can create.
  • Select the console Application from the list.
  • Then at the bottom of windows,there will be three lines , Name location and solution name.
  • Enter the name for your application and browse the location where you want to save it.
  • solution name will be same as your Application name.
  • After giving name, just click on Ok Button.
  • Your Project will be created.


Refer the below diagram.




Write the below code.





using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApplication1

{

         class Program

         {

               static void Main(string[] args)

               {

                     Console.WriteLine("Hello C Sharp");

               }

         }

}





Press the start button in the toolbar to run the program

Output

Hello C Sharp




Here the C Sharp Program to add two integers given by user.

Here there are two integer variables declared to store values to add and third variable to store result.

using System;

namespace addint

{

      class Program

      {

             static void Main(string[] args)

             {

                 int a;

                 int b;

                 int res;

                 Console.WriteLine("Enter the first number to add");

                 a = Convert.ToInt32(Console.ReadLine());

                 Console.WriteLine("Enter the second number to add");

                 b = Convert.ToInt32(Console.ReadLine());

                 res = a + b;

                 Console.WriteLine("Addition of two integers " +a+ " and "+b+ " is " +res);

                 Console.ReadKey();

                }

    }

  }






Output

Enter the first number to add
2
Enter the second number to add
3
Addition of two integers 2 and 3 is 5


Explanation

Using System:- System is the namespace which contains all the required classes and libraries which are useful to write most of the basic programs.

namespace addint:- This namespace is the user defined namespace in which the above program is written.

class program:-class is the keyword defining the class to be used and the Program is the name of the class.

static void main(String [] args):- main() method which is the entry point of Console Application. This is the method where program starts executing. The Strings Array is passed to this method. This Array is used for interacting with Command Line Arguments.

There are three variables declared 'a', 'b' and 'res' for storing two values for adding and third is for storing result of addition.

And Finally the addition is printed on Console.
C Sharp Console Application and sample program of Addition of two integers C Sharp Console Application and sample program of Addition of two integers Reviewed by LanguageExpert on August 10, 2017 Rating: 5

No comments