`csharp using System;namespace MyNamespace { // Replace with your namespace if needed.
class Program { // Replace with the name of your class if not using "Program" as default entry point.
static int Add(int num1, int num2) { // Function definition with name 'Add' and two integer arguments.
return num1 + num2; // Return statement that returns the sum of the numbers.
}
}
}
`You can call this function like: `int result = Add(3, 5);`. The value of `result` will be set to the sum of integers passed as arguments (in this case: 8). write a simple c# calculator Here's a simple console-based calculator program written in C#. This program performs addition, subtraction, multiplication, and division operations based on user input.
`csharp using System;namespace SimpleCalculator
{
class Program
{
static void Main(string[] args)
{
double num1, num2; // Declare two variables to hold the numbers for calculation.
char operation; // Character variable to store an operator (+, -, *, /).
Console.Write("Enter first number: "); // Request user input for the first number.
num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter an operator (+, -, *, /): "); // Request user input for the operation to be performed.
operation = Convert.ToChar(Console.ReadLine()[0]);
Console.Write("Enter second number: "); // Request user input for the second number.
num2 = Convert.ToDouble(Console.ReadLine());
switch (operation) // Perform calculation based on user-input operator.
{
case '+': Console.WriteLine($"Result: {num1 + num2}"); break; // Addition operation.
case '-': Console.WriteLine($"Result: {num1 - num2}"); break; // Subtraction operation.
case '*': Console.WriteLine($"Result: {num1 * num2}"); break; // Multiplication operation.
case '/': if (num2 != 0) Console.WriteLine($"Result: {num1 / num2}"); // Division operation with error handling for division by zero.
else Console.WriteLine("Error! Division by Zero."); break;
default: Console.WriteLine($"Invalid operator '{operation}'"); break; // Handle invalid input for operation character.
}
Console.ReadKey(); // Keep the console window open until a key is pressed before closing it down.
}
}
}
`
Login to Continue, We will bring you back to this content 0