Write a C program that uses functions
to perform the following operations:
- Reading a complex number
- Writing a complex number
- Addition of two complex numbers
- Multiplication of two complex numbers
(Note: represent complex number using a structure.)
Algorithm:
Step 1: Start Step 2: declare structure for complex numbers Step 3: read the complex number Step 4: read choice Step 5: if choice = 1 then addition operation will perform and it contains following steps i) w.realpart = w1.realpart + w2.realpart; ii) w.imgpart = w1.imgpart + w2.imgpart; goto step 4 Step 6: if choice = 2 then multiplication operation will perform and it contains following steps i) w.realpart = (w1.realpart * w2.realpart)-(w1.imgpart * w2.imgpart); ii) w.imgpart = (w1.realpart * w2.imgpart)+(w1.imgpart * w2.realpart); goto step 4 Step 7: if choice = 0 then exit operation will perform Step 8: if w.imgpart > 0 then print realpart+imgpart else Print realpart. Step 9: Stop
Program:
#include <stdio.h> #include <conio.h> struct complex { float real, imag; }a, b, c; struct complex read(void); void write(struct complex); struct complex add(struct complex, struct complex); struct complex sub(struct complex, struct complex); struct complex mul(struct complex, struct complex); struct complex div(struct complex, struct complex); void main () { clrscr(); printf("Enter the 1st complex number\n "); a = read(); write(a); printf("Enter the 2nd complex number\n"); b = read(); write(b); printf("Addition\n "); c = add(a, b); write(c); printf("Substraction\n "); c = sub(a, b); write(c); printf("Multiplication\n"); c = mul(a, b); write(c); printf("Division\n"); c = div(a, b); write(c); getch(); } struct complex read(void) { struct complex t; printf("Enter the real part\n"); scanf("%f", &t.real); printf("Enter the imaginary part\n"); scanf("%f", &t.imag); return t; } void write(struct complex a) { printf("Complex number is\n"); printf(" %.1f + i %.1f", a.real, a.imag); printf("\n"); } struct complex add(struct complex p, struct complex q) { struct complex t; t.real = (p.real + q.real); t.imag = (p.imag + q.imag); return t; } struct complex sub(struct complex p, struct complex q) { struct complex t; t.real = (p.real - q.real); t.imag = (p.imag - q.imag); return t; } struct complex mul(struct complex p, struct complex q) { struct complex t; t.real=(p.real * q.real) - (p.imag * q.imag); t.imag=(p.real * q.imag) + (p.imag * q.real); return t; } struct complex div(struct complex p, struct complex q) { struct complex t; t.real = ((p.imag * q.real) - (p.real * q.imag)) / ((q.real * q.real) + (q.imag * q.imag)); t.imag = ((p.real * q.real) + (p.imag * q.imag)) / ((q.real * q.real) + (q.imag * q.imag)); return(t); }
Input & Output:
Enter the real part 2 Enter the imaginary part 4 Complex number is 2.0 + i4.0 Enter the real part 4 Enter the imaginary part 2 Complex number is 4.0 + i2.0 Addition Complex number is 6.0 + i6.0 Subtraction Complex number is -2.0 + i2.0 Multiplication Complex number is 0.0 + i20.0 Division Complex number is 0.6 + i0.8
-
UpdatedDec 25, 2014
-
Views25,199
You May Like
Write a C program to find the roots of a quadratic equation
.
Write a C program to count the lines, words and characters in a given text.
Write a C program to find the sum of individual digits of a positive integer.
Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use switch statement
)
Write a C program to generate all the prime numbers
between 1 and n, where n is a value supplied by the user.
Write C programs that use both recursive
and non-recursive functions
- To find the
factorial
of a given integer. - To find the
GCD
(greatest common divisor) of two given integers.