Sunday 8 January 2012

C PROGRAMMING EXAMPLES


c program to add two complex numbers

c program to add two complex numbers :- This program calculate the sum of two complex numbers which will be entered by the user and then printed their sum. User will have to enter the real and imaginary parts of two complex numbers. Then on our program we will add real parts and imaginary parts of complex numbers and then we add them and prints the complex number, i is the symbol used for iota. For example if user entered two complex numbers as
(1 + 2i) and (4 + 6 i) then output of program will be (5+8i).

C programming code


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 
struct complex
{
   int real;
   int img;
};
 
main()
{
   struct complex a, b, c;
 
   printf("Enter a and b where a + ib is the first complex number.");
   printf("\na = ");
   scanf("%d", &a.real);
   printf("b = ");
   scanf("%d", &a.img);
   printf("Enter c and d where c + id is the second complex number.");
   printf("\nc = ");
   scanf("%d", &b.real);
   printf("d = ");
   scanf("%d", &b.img);
 
   c.real = a.real + b.real;
   c.img = a.img + b.img;
 
   if ( c.img >= 0 )
      printf("Sum of two complex numbers = %d + %di",c.real,c.img);
   else
      printf("Sum of two complex numbers = %d %di",c.real,c.img);
 
   getch();
   return 0;
}



output of c program to add two complex numbers

No comments:

Post a Comment