Addition of Two Numbers in C# Program | Minimum Variables
ADVERTISEMENTS
Addition of two numbers in C# program.
In this article, you will learn how to make the addition of two numbers in C# program.
Example
Enter two integer values::
5
7
Result:: 5 + 7 = 12
You should have knowledge of the following topics in C# programming to understand this program:
- C# Oops Concepts
- C# System Library
Addition of Two Numbers in C# Program with Integer Values
// Addition of Two Numbers in C# Program with Integer Values
using System;
public class W3CW {
// @It's the driver function
static public void Main (string[] args) {
Console.WriteLine("Enter two integer values::");
int p = Convert.ToInt32(Console.ReadLine());
int q = Convert.ToInt32(Console.ReadLine());
// It will calculate the sum
int r = p + q;
// It will print the final output
Console.WriteLine("\nResult:: " + p + " + " + q + " = " + r);
}
}
Output
Enter two integer values::
5
7
Result:: 5 + 7 = 12
Explanation
In the above program, we have taken two inputs 5
and 7
from the user via the system console. Then we applied the arithmetic operation on these values after that It will return the value 12
the output of the program.
Addition of Two Numbers in C# Program with Minimum Variables
// Addition of Two Numbers in C# Program with Minimum Variables
using System;
public class W3CW {
// @It's the driver function
static public void Main (string[] args) {
Console.WriteLine("Enter two integer values::");
int p = Convert.ToInt32(Console.ReadLine());
int q = Convert.ToInt32(Console.ReadLine());
Console.Write("\nResult:: " + p + " + " + q + " = ");
// It will calculate the sum
p = p + q;
// It will print the final output
Console.WriteLine(p);
}
}
Output
Enter two integer values::
5
7
Result:: 5 + 7 = 12
Addition of Two Numbers in C# Program with Float Values
// Addition of Two Numbers in C# Program with Float Values
using System;
public class W3CW {
// @It's the driver function
static public void Main (string[] args) {
Console.WriteLine("Enter two float values::");
float p = float.Parse(Console.ReadLine());
float q = float.Parse(Console.ReadLine());
Console.Write("\nResult:: " + p + " + " + q + " = ");
// It will calculate the sum
p = p + q;
// It will print the final output
Console.WriteLine(p);
}
}
Output
Enter two float values::
5.23
7.29
Result:: 5.23 + 7.29 = 12.52