Users Online

· Guests Online: 153

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Program to Add Two Complex Numbers

C# Program to Add Two Complex Numbers

 

 

This is a C# Program to add 2 complex numbers.

Problem Description

This C# Program Adds two Complex Numbers.

Problem Solution

Group the real part of the complex number and the imaginary part of the complex number and then add.

Program/Source Code

Here is source code of the C# program that Adds two Complex Numbers.The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Add 2 Complex Numbers
 */
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 c1, Complex c2)
    {
        return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
    }
 
 
    public override string ToString()
    {
        return (String.Format("{0} + {1}i", real, imaginary));
    }
}
 
class TestComplex
{
    static void Main()
    {
        Complex num1 = new Complex(2, 3);
        Complex num2 = new Complex(3, 4);
        Complex sum = num1 + num2;
        Console.WriteLine("First Complex Number :  {0}", num1);
        Console.WriteLine("Second Complex Number : {0}", num2);
        Console.WriteLine("The Sum of the Two Numbers : {0}", sum);
        Console.ReadLine();
    }
}
Program Explanation

This C# program is used to add two complex numbers. We have already defined the first complex and second complex number using ‘num1’ and ‘num2’ variables. Compute the summation of the values ‘num1’ and ‘num2’ variable. Group the real part and the imaginary part of the complex number and then add the value. Print the addition of two complex numbers.

 
Runtime Test Cases
 
First Complex Number : 2+3i
Second Complex Number : 3+4i
The Sum of the Two Numbers : 5+7i

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.85 seconds
10,813,397 unique visits