Program to add two complex numbers

Description

To add two complex number, add the real part of first complex number to the real part of the
second complex number. Likewise add imaginary part of first complex number to the imaginary
part of second complex number.

C/C++

/* C Program to add two complex numbers */
//Save it as AddComplexNumbers.c


#include <stdio.h>

struct complexNum
{
   float real, img;
};

int main()
{
    struct complexNum firstNum, secondNum, sum;

    printf("Enter the real part of first complex number : ");
    scanf("%f",&firstNum.real);
    printf("Enter the imaginary part of first complex number : ");
    scanf("%f",&firstNum.img);

    printf("Enter the real part of second complex number : ");
    scanf("%f",&secondNum.real);
    printf("Enter the imaginary part of second complex number : ");
    scanf("%f",&secondNum.img);

    sum.real = firstNum.real + secondNum.real;
    sum.img = firstNum.img + secondNum.img;

    if(sum.img>=0){
        printf("\nThe complex number after addition %.3f+%.3fi",sum.real,sum.img);
    }
    else{
        printf("\nThe complex number after addition %.3f%.3fi",sum.real,sum.img);
    }

    return 0;
}
Input:
Enter the real part of first complex number : 6
Enter the imaginary part of first complex number : -6
Enter the real part of second complex number : 3
Enter the imaginary part of second complex number : 2

Output:
The complex number after addition : 9-4i

Java

/* Java Program to add two complex numbers */
//Save it as AddComplexNumbers.java

import java.io.*;
import java.util.Scanner;

class ComplexNum { 
    int real, img; 
 
    ComplexNum() { 
    } 
    
    ComplexNum(int real, int img) 
    { 
        this.real = real; 
        this.img = img; 
    } 
    
    ComplexNum addComp(ComplexNum firstNum, ComplexNum secondNum) 
    {  
        ComplexNum temp = new ComplexNum(); 
 
        temp.real = firstNum.real + secondNum.real; 
 
        temp.img = firstNum.img + secondNum.img; 
 
        return temp; 
    } 
} 

public class AddComplexNumbers { 

    public static void main(String[] args) 
    { 
        
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the real part of first complex number : ");
        int real1 = scanner.nextInt();
        System.out.println("Enter the imaginary part of first complex number : ");
        int img11 = scanner.nextInt();
        
        System.out.println("Enter the real part of second complex number : ");
        int real2 = scanner.nextInt();
        System.out.println("Enter the imaginary part of second complex number : ");
        int img12 = scanner.nextInt();

        ComplexNum firstNum = new ComplexNum(real1, img11); 

        ComplexNum secondNum = new ComplexNum(real2, img12); 

        ComplexNum sum = new ComplexNum(); 

        sum = sum.addComp(firstNum, secondNum); 

        if(sum.img>=0) {
            System.out.println("The complex number after addition : " + sum.real+" + " + sum.img+"i"); 
        }
        else {
            System.out.println("The complex number after addition : " + sum.real+ sum.img+"i");
        }
    } 
}
Input:
Enter the real part of first complex number : 
6
Enter the imaginary part of first complex number : 
-6
Enter the real part of second complex number : 
3
Enter the imaginary part of second complex number : 
2

Output:
The complex number after addition : 9-4i

Related programs

1) Program to find modulus of two numbers
2) Program to calculate power of a number
3) Program to calculate Grade of Student
4) Program to find the average of numbers in a given range
5) Program to Swap Two Numbers
6) Program to find sum of digits of a number
7) Program to reverse a number
8) Program to display multiplication table of a number
9) Program to Display Fibonacci Series using Recursion
10) Program to find factors of a Number
Share Me

Leave a Reply