
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Complex Numbers in C#
To work WITH and display complex numbers in C#, you need to check for real and imaginary values.
A complex number like 7+5i is formed up of two parts, a real part 7, and an imaginary part 5. Here, the imaginary part is the multiple of i.
To display complete numbers, use the −
public struct Complex
To add both the complex numbers, you need to add the real and imaginary part −
public static Complex operator +(Complex one, Complex two) { return new Complex(one.real + two.real, one.imaginary + two.imaginary); }
You can try to run the following code to work with complex numbers in C#.
Example
using System; public struct Complex { public int real; public int imaginary; public Complex(int real, int imaginary) { this.real = real; this.imaginary = imaginary; } public static Complex operator +(Complex one, Complex two) { return new Complex(one.real + two.real, one.imaginary + two.imaginary); } public override string ToString() { return (String.Format("{0} + {1}i", real, imaginary)); } } class Demo { static void Main() { Complex val1 = new Complex(7, 1); Complex val2 = new Complex(2, 6); // Add both of them Complex res = val1 + val2; Console.WriteLine("First: {0}", val1); Console.WriteLine("Second: {0}", val2); // display the result Console.WriteLine("Result (Sum): {0}", res); Console.ReadLine(); } }
Output
First: 7 + 1i Second: 2 + 6i Result (Sum): 9 + 7i
- Related Articles
- Complex Numbers in Python?
- Complex numbers in C++
- Complex Numbers in Java
- Complex numbers in C++ programming
- Complex numbers operations in Arduino
- Geometry using Complex Numbers in C++
- Python program for Complex Numbers
- How to Plot Complex Numbers in Python?
- Proj() function for Complex Numbers in C++
- How can we use complex numbers in Python?
- Program to Add Two Complex Numbers in C
- How to add two Complex numbers in Golang?
- Java Program to Add Two Complex numbers
- Haskell program to add two complex numbers
- Kotlin Program to Add Two Complex numbers

Advertisements