Program to Add Two Integers - (C# Basic Programs)
Adding two integers is a simple programming exercise that helps beginners learn the basics of programming concepts, data types, and functions. In this tutorial, we will discuss a program to add two integers using C# programming language.
Syntax
The syntax for adding two integers in C# is as follows:
int sum = num1 + num2;
Example
using System;
namespace AddTwoIntegers
{
class Program
{
static void Main(string[] args)
{
int num1, num2, sum;
Console.WriteLine("Enter the first number: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the second number: ");
num2 = Convert.ToInt32(Console.ReadLine());
sum = num1 + num2;
Console.WriteLine("The sum is: " + sum);
Console.ReadLine();
}
}
}
Output:
Enter the first number:
10
Enter the second number:
20
The sum is: 30
Explanation
The above C# program prompts the user to enter two integers and reads them using the Console.ReadLine()
method. These integers are then added using the addition operator +
and stored in the variable sum
. The value of sum
is then displayed to the user using the Console.WriteLine()
method.
Use
The program to add two integers is a basic exercise that can help beginners to get started with learning the basics of C# programming. This program allows users to practice input/output operations, basic arithmetic operations, and variables.
Summary
In this tutorial, we have discussed a program to add two integers using C# programming language. We have seen the syntax, example, explanation, and use of adding two integers in C#. Programs like this are essential for beginners to practice programming concepts and build a solid foundation in coding fundamentals.