C/Complex Form To Trig Form.c
2024-05-13 11:09:46 -05:00

63 lines
1.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Complex Form to trig Form
*
* A prgorgam that converts complex form to trig form
* givien 𝑎 and 𝑏.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
float a, b; /* First Nomial and Coeficient to 𝑖 */
/* Print formulas */
printf("Complex Form --> Trig Form\n");
printf("Complex Form: 𝑎 + 𝑏𝑖\n");
printf("Trig Form: 𝑟(cosθ + 𝑖sinθ)\n");
printf("𝑎, 𝑏 != 0\n");
/* Prompt user for variables */
printf("Enter 𝑎: ");
scanf("%f", &a);
printf("Enter 𝑏: ");
scanf("%f", &b);
/* Sanity Check
*
* If 𝑎 or 𝑏 equal 0, exit the program.
*/
if (a == 0 || b == 0) {
printf("Error: 𝑎 or 𝑏 cannot = 0\n");
exit(1);
};
/* Calculate values of additional variables */
float r = sqrt(a * a + b * b); /* 𝑟 */
float t = atan(b / a); /* θ */
/* Check for the quadrent of the number,
* and add the appropriate value to θ.
*
* Quadrent I: Add nothing
* Quadrent II or III: Add pi
* Quadrent IV: Add 2 * pi
*/
if (a < 0) {
/* Add pi */
t = t + M_PI;
} else {
if (b < 0) {
/* Add 2 * pi */
t = t + 2 * M_PI;
}
}
/* Print number in trigonomic form */
printf("%.2f(cos(%.2f) + 𝑖sin(%.2f))\n", r, t, t);
return 0;
}