Developing a C Program for Complex Number Calculations and Conversions

Verified

Added on  2019/09/25

|14
|1822
|263
Homework Assignment
AI Summary
This report details a C program designed to perform arithmetic operations on complex numbers. The program allows users to add, subtract, multiply, and divide both real and complex numbers in rectangular form. It also includes functionality to convert complex numbers between rectangular and polar forms. The program utilizes standard C libraries, including `stdio.h`, `stdlib.h`, `conio.h`, and `math.h`, and defines the value of PI for calculations. The code presents a menu-driven interface, enabling users to select the desired operation. The multiplication and division of complex numbers are implemented using the standard formulas, and the conversion to polar form involves calculating magnitude and angle using trigonometric functions such as `atan()`. The report includes the source code and screenshots of the program's output demonstrating its functionality across various operations. The program is a practical demonstration of complex number arithmetic and its implementation in C.
Document Page
Report
To multiply two complex numbers I use the formula
Here, (xu yv), the real part of the product, is the product of the real parts minus the
product of the imaginary parts, but (xv + yu), the imaginary part of the product, is the
sum of the two products of one real part and the other imaginary part.
I used the steps which are required to divide complex numbers:
Step 1: To divide complex numbers, you must multiply by the conjugate. To
find the conjugate of a complex number all you have to do is change the
sign between the two terms in the denominator.
Step 2: Distribute (or FOIL) in both the numerator and denominator to remove
the parenthesis.
Step 3: Simplify the powers of i, specifically remember that i2 = –1.
Step 4: Combine like terms in both the numerator and denominator, that is,
combine real numbers with real numbers and imaginary numbers with
imaginary numbers.
Step 5: Write you answer in the form a + bi.
I used the formula to divide two complex numbers :
In component notation with ,
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
To convert rectangular form of complex number to polar form I used the formula
We have a + ib
Then polar form is sqrt(a*a + b*b) , atan(b/a)*180 / PI
In C PI is defined in math.h: #define PI 3.14159265358979323846
I use atan(), asin(), and acos(). Those are the respective inverses of tan, sin, and cos.
Document Page
Code :
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#define PI 3.14159265358979323846
int main()
{
printf("Welcome!\n");
int n;// choice of the user will be store in n
float a, b, c, d;
while(1) // This loop will run infinitely but it will break when user enter 9 to quit
{
printf("************************************************************************
******************************************\n");
printf("Menu\n");
printf("Please enter\n");
printf("1 to add two real numbers and display the result\n");
Document Page
printf("2 to subtract two real numbers and display the result\n");
printf("3 to multiply two real numbers and display the result\n");
printf("4 to divide two real numbers and display the result\n");
printf("5 to add two complex numbers in the form a +- jb and c +- jd and display the
result in rectangular notation\n");
printf("6 to subtract two complex numbers in the form a +- jb and c +- jd and display the
result in rectangular notation\n");
printf("7 to multiply two complex numbers in the form a +- jb and c +- jd and display the
result in rectangular notation\n");
printf("8 to divide two complex numbers in the form a +- jb and c +- jd and display the
result in rectangular notation\n");
printf("9 to quit \n");
printf("Please enter your choice : ");
scanf("%d", &n);//n is used to store the choice of the user
if(n == 9) // if user enters 9 then break the loop and hence quit the program
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
break;
if(n == 1)
{
printf("Please enter first number : ");
scanf("%f", &a);
printf("Please enter second number : ");
scanf("%f", &b);
printf("Their sum is %f \n", (a+b));
}
if(n == 2)
{
printf("Please enter first number : ");
scanf("%f", &a);
printf("Please enter second number : ");
scanf("%f", &b);
printf("Their difference is i.e a-b = %f \n", (a-b));
Document Page
}
if(n == 3)
{
printf("Please enter first number : ");
scanf("%f", &a);
printf("Please enter second number : ");
scanf("%f", &b);
printf("Their product is %f \n", (a*b));
}
if(n == 4)
{
printf("Please enter first number : ");
scanf("%f", &a);
printf("Please enter second number : ");
scanf("%f", &b);
printf("Their division is i.e a/b = %f \n", (a/b));
Document Page
}
if(n == 5)
{
printf("Please enter real part of first number : ");
scanf("%f", &a);
printf("Please enter imaginary part of first number : ");
scanf("%f", &b);
printf("Please enter real part of second number : ");
scanf("%f", &c);
printf("Please enter imaginary part of second number : ");
scanf("%f", &d);
printf("Their sum in rectangular form is %f + j%f \n", (a+c), (b+d));
float k = a+c;
float l = b+d;
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
printf("The result in polar form : magnitude is %f and angle is %f degrees \n", sqrt(k*k
+ l*l) , atan(l/k)*180 / PI);
}
if(n == 6)
{
printf("Please enter real part of first number : ");
scanf("%f", &a);
printf("Please enter imaginary part of first number : ");
scanf("%f", &b);
printf("Please enter real part of second number : ");
scanf("%f", &c);
printf("Please enter imaginary part of second number : ");
scanf("%f", &d);
printf("Their difference in rectangular form is %f + j%f \n", (a-c), (b-d));
Document Page
float k = a-c;
float l = b-d;
printf("The result in polar form : magnitude is %f and angle is %f degrees \n", sqrt(k*k
+ l*l) , atan(l/k)*180 / PI);
}
if(n == 7)
{
printf("Please enter real part of first number : ");
scanf("%f", &a);
printf("Please enter imaginary part of first number : ");
scanf("%f", &b);
printf("Please enter real part of second number : ");
scanf("%f", &c);
printf("Please enter imaginary part of second number : ");
scanf("%f", &d);
Document Page
//Formula to multiply two complex numbers is : (x+yi)(u+vi) = (xu=yv)+(xv+yu)i
printf("Their product in rectangular form is %f + j%f \n", (a*c - b*d), (a*d + b*c));
float k = (a*c - b*d);
float l =(a*d + b*c);
printf("The result in polar form : magnitude is %f and angle is %f degrees \n", sqrt(k*k
+ l*l) , atan(l/k)*180 / PI);
}
if(n == 8)
{
printf("Please enter real part of first number : ");
scanf("%f", &a);
// The formula to divide two complex numbers is given by
// ((a,b))/((c,d))=((ac+bd)/(c^2+d^2),(bc-ad)/(c^2+d^2)).
printf("Please enter imaginary part of first number : ");
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
scanf("%f", &b);
printf("Please enter real part of second number : ");
scanf("%f", &c);
printf("Please enter imaginary part of second number : ");
scanf("%f", &d);
// The formula to divide two complex numbers is given by
// ((a,b))/((c,d))=((ac+bd)/(c^2+d^2),(bc-ad)/(c^2+d^2)).
float denom = c*c + d*d; // here denominator is denom
printf("Their division in rectangular form is %f + j%f \n", (a*c + b*d)/denom, (b*c -
a*d)/denom);
float k = (a*c + b*d)/denom;
float l =(b*c - a*d)/denom;
printf("The result in polar form : magnitude is %f and angle is %f degrees \n", sqrt(k*k
+ l*l) , atan(l/k)*180 / PI);
Document Page
}
}// end of while loop
return 0;
}//end of main
Screenshots of outputs :
chevron_up_icon
1 out of 14
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]