Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Monday 9 July 2018

C program : Add two complex numbers

#include <stdio.h>
struct complex
{
   int real, img;
};
int main()
{
   struct complex a, b, c;
        printf("Please enter first complex number\n");  
       printf("Enter Real part of the 1st complex   number\n");
      scanf("%d", &a.real);
     printf("Enter Imaginary part of the 1st complex    number without i\n");
    scanf("%d", &a.img);
    printf("Please enter second complex number\n");
   printf("Enter Real part of the 2nd complex number\n");
 scanf("%d", &b.real);
 printf("Enter Imaginary part of the 2nd complex number without i\n");
scanf("%d", &b.img);
c.real = a.real + b.real;
c.img = a.img + b.img;
if ( c.img >= 0 )
 printf("The sum of two complex numbers = %d + %di\n",c.real,c.img);
else
printf("The sum of two complex numbers = %d %di\n",c.real,c.img);
 return 0;
}

No comments:

Post a Comment