Tuesday, February 10, 2015

Complex Numbers - Add, Subtract, Divide & Multiply

/* Add, Subtract, Divide & Multiply Complex Numbers */ #include <stdio.h> struct complexNumber { float real, img; }; typedef struct complexNumber complexNum; complexNum add(complexNum, complexNum); complexNum sub(complexNum, complexNum); complexNum divide(complexNum, complexNum); complexNum multiply(complexNum, complexNum); int main() { complexNum ab, cd, result; ab.real = 0; ab.img = 0; cd.real = 0; cd.img = 0; printf("Enter real part of first number: "); scanf("%f", &ab.real); printf("Enter imaginary part of first number: "); scanf("%f", &ab.img); printf("Enter real part of second number: "); scanf("%f", &cd.real); printf("Enter imaginary part of second number: "); scanf("%f", &cd.img); result = add(ab, cd); printf("\nAddition\n"); printf("Real: %.1f\n", result.real); printf("Imaginary: %.1f\n", result.img); printf("\n\n"); result = sub(ab, cd); printf("Subtraction\n"); printf("Real: %.1f\n", result.real); printf("Imaginary: %.1f\n", result.img); printf("\n\n"); result = divide(ab, cd); printf("Division\n"); printf("Real: %.1f\n", result.real); printf("Imaginary: %.1f\n", result.img); printf("\n\n"); result = multiply(ab, cd); printf("Multiplication\n"); printf("Real: %.1f\n", result.real); printf("Imaginary: %.1f\n", result.img); printf("\n\n"); } complexNum add(complexNum ab, complexNum cd) { complexNum result; result.real = ab.real + cd.real; result.img = ab.img + cd.img; return result; } complexNum sub(complexNum ab, complexNum cd) { complexNum result; result.real = ab.real - cd.real; result.img = ab.img - cd.img; return result; } complexNum divide(complexNum ab, complexNum cd) { complexNum result; float a, b, c, d; a = ab.real; b = ab.img; c = cd.real; d = ab.img; result.real = (((a*c) + (b*d)) / ((c*c) + (d*d))); result.img = (((b*c) - (a*d)) / ((c*c) + (d*d))); return result; } complexNum multiply(complexNum ab, complexNum cd) { complexNum result; float a, b, c, d; a = ab.real; b = ab.img; c = cd.real; d = ab.img; result.real = ((a*c) - (b*d)); result.img = ((b*c) + (a*d)); getch(); return result; }

Share

& Comment

0 comments:

Post a Comment

 

Copyright © 2015 Code Fervor™ is a registered trademark.

Designed by Templateism By Naman Kumar