Add two numbers

This tutorial will demonstrate how to read from the input console (console line) to answer the prompts. The prompts will need to be a integer value to add the two inputs together.

I am using the System.Console object to read from the console and then converting the string inputs into integers with the Convert.ToInt32 function (the System.Convert has many methods and ToInt32 is one of these).

The code

using System;       // for the console object
 
class addtwonumbers
{
       public static void Main()
       {
              try 
              {
                     // output to the console
                     Console.Write("Please enter value 1 : ");
                     // readin the console input, and then convert to a integer value
                     int val1 = Convert.ToInt32(Console.ReadLine());
                     Console.Write("Please enter value 2 : ");
                     int val2 = Convert.ToInt32(Console.ReadLine());
                     // write out the answer 
                     Console.WriteLine("Answer = " + (val1 + val2));
              }
              catch (Exception e)
              {
                     // any errors. Like converting a string to a integer value
                     Console.WriteLine("Error : " + e.ToString());
              }
       }
}

save that as addtwonumbers.cs.

Once compiled (mcp addtwonumbers.cs with mono) and executed (mono addtwonumbers.exe) the output will be

Please enter value 1 : 30
Please enter value 2: 23
Answer = 53

Tags: , , ,

10 Responses to “Add two numbers”

  1. A.Sundeep KUmar says:

    class addtwonumbers
    {
    public static void Main(string[] args)
    {
    int a,b,c;
    System.Console.WriteLine(“Enter the value of a=”);
    a=int.Parse(System.Console.ReadLine());
    System.Console.WriteLine(“Enter the value of b=”);
    b=int.Parse(System.Console.ReadLine());
    c=a+b;
    System.Console.WriteLine(“Sum is “+c);
    }
    }

  2. satya says:

    thanks to sudeep kumar………for giving code in easy way to understand

  3. pravin says:

    good code easy to understand

  4. lakmali says:

    Thank you sudeep

  5. ahmed says:

    i want some good examples

  6. ahmed says:

    give some how to display names in c#

  7. cham says:

    tnx a lot :)

  8. nitin says:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Enter d size of array :");
     
                int size = int.Parse(Console.ReadLine());
     
     
                int[] arr = new int[size];
     
                for (int i = 0; i < size; i++)
                {
                    Console.WriteLine("enter d elements according to size :");
                    arr[i] = Convert.ToInt32(Console.ReadLine());
     
     
                }
                Console.WriteLine("Finished:");
     
                foreach (int i in arr)
                {
                    Console.Write(" " +i);
     
                }
                Console.WriteLine("sort the array :");
                Array.Sort(arr);
     
                foreach (int i in arr)
                    Console.WriteLine(i);
     
                Console.WriteLine(); 
     
     
     
     
     
     
     
                /*  int a, b, c;
                Console.WriteLine("Enter value no1 :");
                a = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Enter value no2 :");
                b = Convert.ToInt32(Console.ReadLine());
     
                c = a + b;
                Console.WriteLine(c);
     
     
                /*int a, b, c;
     
                Console.WriteLine("enter the value of a=");
                a = int.Parse(Console.ReadLine());
                Console.WriteLine("enter the value of b=");
                b = int.Parse(Console.ReadLine());
                c = a + b;
                Console.WriteLine("c= :"+c);
     
     
     
     
     
              /* string[] arr=new string[2];
     
               arr[0] = "nits";
               arr[1] = "nitin";
     
               foreach (string s in arr)
                   Console.WriteLine(s);
     
     
     
     
     
                /* int[] arr = new int[5];
     
                int i;
     
                for (i = 0; i < 5; i++)
                    arr[i] = i;
     
                for (i = 0; i < 5; i++)
                    Console.WriteLine("arr[ "+ i +"]: "+ arr[i]);
                */
     
     
     
     
               /* int avg = 0;
                int[] arr1 = new int[5];
     
                    arr1[0]=5;
                    arr1[1]=10;
                    arr1[2]=5;
                    arr1[3]=5;
                    arr1[4]=5;
     
     
                for (int i=0; i< 5; i++)
                     avg = avg + arr1[i];
     
     
                        avg = avg/5;
     
                    Console.WriteLine("average is "+avg);*/
            }
        }
    }
  9. darshi says:

    Thanx :)

  10. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace addingTwoNumbersInconsolApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                int a, b, c;
                Console.WriteLine("enter the two Numbers ");
                a = int.Parse(Console.ReadLine());
                b = int.Parse(Console.ReadLine());
                c = a + b;
                Console.WriteLine("sum of two Numbers=" + c);
                Console.ReadLine();
            }
        }
    }

Leave a Reply