find and solve ||
Please wait.....

C# Example Program to Add Two Numbers ( User Input )

This c# tutorial demonstrates how to get two inputs from the user within a Console Application. you will learn how to add these numbers. Add Two Numbers Value Enter By User in Console Application.

You are using the Convert.ToInt32() function over the Console.ReadLine() function is because the Console.ReadLine() function receives the value as String, so to convert it into a number we are using the Convert.ToInt32().

Sum of digits algorithm

To get a sum of each digit by C# program, use the following algorithm:

First, download and install Visual Studio on Windows Machine and open your visual studio.

Create a C# Console Application

  • Open your visual studio 2019 or greater than 2019
  • Click on Create a New Project
  • Select Console Application and then Next
  • Provide a Project name and confirm or change the Location. Click Next

  • Select the Target Framework under drop-down (.NET Core 5.0) and Click on Create button.

C# Program to Add / Sum Two Numbers from user input

using System;

// namespace declaration
namespace SumExample
    // declaration class here
    public class FindAndSolve
        // This is Main Method
        public static void Main(string[] args)
            //declare int type variable
            int firstNumber, secondNumber, sum;
            Console.WriteLine("Enter the First Number");
            //user input
            firstNumber = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the Second Number");
            //user intput
            secondNumber = Convert.ToInt32(Console.ReadLine());
            //sum two number
            sum = firstNumber + secondNumber;
            Console.WriteLine("Your first number is {0}", firstNumber);
            Console.WriteLine("Your second number is {0}", secondNumber);
            Console.WriteLine("The sum of two number is {0}",sum);


Explanation Given Above Example

  • using System: System is a namespace that contains the commonly used types. It is specified with a using System directive.
  • namespace SumExample: namespace is the keyword that is used to define the namespace. SumExample the user-defined name given to namespace. For more details, C# Namespaces
  • class FindAndSolve: class is the keyword that is used for the declaration of classes. FindAndSolve is the user-defined name of the class.
  • static void Main(string[] args): Main() is a method of class FindAndSolve. The execution of every C# program starts from the Main() method.
  • Console.WriteLine(): WriteLine() is a method of the Console class defined in the System namespace.
  • Console.ReadKey(): This is for the VS.NET Users. This makes the program wait for a keypress and prevents the screen from running and closing quickly.
  • firstNumber: This is Integers type variable
  • secondNumber: This is also Integers type variable
  • Int: Integer is a keyword that is used to declare a variable that can store an integral type of value (signed integer) the range from -2,147,483,648 to 2,147,483,647. It is an alias of the System.Int32.