GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — C# - Arithmetic Operators
Selected Reading
C# - Arithmetic OperatorsWhat Are Arithmetic Operators in C#?C# arithmetic operators perform mathematical operations such as addition, subtraction, multiplication, division, and modulus between numeric data types. These operators are binary operators, which need two operands to perform the operations. List of C# Arithmetic OperatorsFollowing table shows all the arithmetic operators supported by C#. Assume variable A holds 10 and variable B holds 20, then −
Example of Arithmetic OperatorsThe following example demonstrates all the arithmetic operators available in C# − using System;
namespace OperatorsAppl {
class Program {
static void Main(string[] args) {
int a = 21;
int b = 10;
int c;
c = a + b;
Console.WriteLine("Line 1 - Value of c is {0}", c);
c = a - b;
Console.WriteLine("Line 2 - Value of c is {0}", c);
c = a * b;
Console.WriteLine("Line 3 - Value of c is {0}", c);
c = a / b;
Console.WriteLine("Line 4 - Value of c is {0}", c);
c = a % b;
Console.WriteLine("Line 5 - Value of c is {0}", c);
c = a++;
Console.WriteLine("Line 6 - Value of c is {0}", c);
c = a--;
Console.WriteLine("Line 7 - Value of c is {0}", c);
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result − Line 1 - Value of c is 31 Line 2 - Value of c is 11 Line 3 - Value of c is 210 Line 4 - Value of c is 2 Line 5 - Value of c is 1 Line 6 - Value of c is 21 Line 7 - Value of c is 22 More Examples of Arithmetic OperatorsExample 1: Basic Arithmetic OperationsIn this example, we are performing basic arithmetic operations such as addition, subtraction, multiplication, division, and modulus. using System;
class Program
{
static void Main()
{
int a = 10, b = 3;
Console.WriteLine("Addition: " + (a + b));
Console.WriteLine("Subtraction: " + (a - b));
Console.WriteLine("Multiplication: " + (a * b));
Console.WriteLine("Division: " + (a / b));
Console.WriteLine("Modulus: " + (a % b));
}
}
In this example, we are checking whether it is not raining to decide if we should go outside. Addition: 13 Subtraction: 7 Multiplication: 30 Division: 3 Modulus: 1 Example 2: Integer vs. Floating-Point DivisionIn this example, we are comparing integer division with floating-point division to understand the difference. using System;
class Program
{
static void Main()
{
int a = 7, b = 2;
double c = 7, d = 2;
Console.WriteLine("Integer Division: " + (a / b));
Console.WriteLine("Floating-Point Division: " + (c / d));
}
}
In this example, we are checking whether it is not raining to decide if we should go outside. Integer Division: 3 Floating-Point Division: 3.5 Use CasesThe following are the real-world use cases of arithmetic operators in C#: Example 3: Calculating the Area of a CircleIn this example, we are calculating the area of a circle using the formula r. using System;
class Program
{
static void Main()
{
const double Pi = 3.14159;
double radius = 5.5;
double area = Pi * radius * radius;
Console.WriteLine("Circle Area: " + area);
}
}
In this example, we are checking whether it is not raining to decide if we should go outside. Circle Area: 95.0330975 Example 4: Converting Temperature (Celsius to Fahrenheit)In this example, we are converting a temperature value from Celsius to Fahrenheit. using System;
class Program
{
static void Main()
{
double celsius = 30;
double fahrenheit = (celsius * 9 / 5) + 32;
Console.WriteLine("Temperature in Fahrenheit: " + fahrenheit);
}
}
In this example, we are checking whether it is not raining to decide if we should go outside. Temperature in Fahrenheit: 86 Example 5: Calculating Monthly Loan PaymentIn this example, we are calculating the monthly loan payment using a standard loan payment formula. using System;
class Program
{
static void Main()
{
double principal = 10000; // Loan amount
double rate = 5.5 / 100 / 12; // Monthly interest rate
int months = 60; // Loan term
double monthlyPayment = (principal * rate) / (1 - Math.Pow(1 + rate, -months));
Console.WriteLine("Monthly Loan Payment: " + monthlyPayment);
}
}
In this example, we are checking whether it is not raining to decide if we should go outside. Monthly Loan Payment: 190.34 Best PracticesThe following are the best practices while working with numerical calculations.
Frequently Asked QuestionsQ1: What happens if I divide by zero in C#?
Q2: Whats the difference between
| ||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |