Knowee
Questions
Features
Study Tools

operations on complex numbers Write a program to find out the addition and multiplication of two complex numbers using structures and functions. Note: Write the code in the functions readC(), display(), add() and product().

Question

operations on complex numbers Write a program to find out the addition and multiplication of two complex numbers using structures and functions.

Note: Write the code in the functions readC(), display(), add() and product().

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a simple C program that uses structures and functions to perform addition and multiplication of two complex numbers.

#include<stdio.h>

// Define the structure for complex number
typedef struct complex {
    float real;
    float imag;
} complex;

// Function to read complex number
complex readC() {
    complex c;
    printf("Enter real and imaginary parts: ");
    scanf("%f %f", &c.real, &c.imag);
    return c;
}

// Function to display complex number
void display(complex c) {
    printf("%.1f + %.1fi\n", c.real, c.imag);
}

// Function to add two complex numbers
complex add(complex n1, complex n2) {
    complex temp;
    temp.real = n1.real + n2.real;
    temp.imag = n1.imag + n2.imag;
    return temp;
}

// Function to multiply two complex numbers
complex product(complex n1, complex n2) {
    complex temp;
    temp.real = n1.real*n2.real - n1.imag*n2.imag;
    temp.imag = n1.imag*n2.real + n1.real*n2.imag;
    return temp;
}

int main() {
    complex n1, n2, result;

    printf("Enter first complex number:\n");
    n1 = readC();

    printf("Enter second complex number:\n");
    n2 = readC();

    result = add(n1, n2);
    printf("The addition of two complex numbers is: ");
    display(result);

    result = product(n1, n2);
    printf("The multiplication of two complex numbers is: ");
    display(result);

    return 0;
}

This program first reads two complex numbers using the readC() function. Then it adds and multiplies them using the add() and product() functions respectively. The results are then displayed using the display() function.

This problem has been solved

Similar Questions

Create a C++ programme that accomplishes all arithmetic operations on two complex objects, including addition, subtraction, multiplication, and division. (X + i * Y) represents the Complex Object (Where X and Y are two real numbers and 'i' is an Imaginary). As a programmer, create a class called "COMPLEX" containing the data members X and Y and also getData() and putData() methods to pefrom read and write for the above said Data Members. Define a separate functions for the operations such as Addition, Subtraction, Multiplication, and Division.Input :Read the Two Complex Objects (Each with X and Y)Output :Addition of Two Complex ObjectsSubtraction of Two Complex ObjectsMultiplication of Two Complex ObjectsDivision of Two Complex ObjectsNote :Both X and Y are Non-Negative, otherwise print "Invalid"Define a separate functions with return type and with arguments of two complext objects.

Write a program overloading arithmetic operators to add two complex numbers using oops c++

Create a Complex class to represent complex numbers. Overload the addition (+), subtraction (-), multiplication (*), and division (/) operators for complex numbers. The prompt ask 2 complex number from the user and display the respective output for the user input.

Write a Java program to find the addition, subtraction, multiplication, and division of two complex numbers using inheritance. Create an abstract class called Complex. From this extends the child classes Addition, Subtraction, Multiplication, and Division. All of these child classes should contain the same methods.Input format :The input consists of four double-type variables, which denote the real and imaginary parts of the first and second complex numbers, respectively.Output format :The output consists of the addition, subtraction, multiplication, and division of the two complex numbers.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :2 64 2Output 1 :Addition: 6.0000 +8.0000 iSubtraction: -2.0000 +4.0000 iMultiplication: -4.0000 +28.0000 iDivision: 1.0000 +1.0000 i

Write a class for addition of complex numbers. The program should ask for real and imaginary part of complex numbers and using function it should add the real part to real part of the number and imaginary part to imaginary part of the number.

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.