Write a programe for complex number using C language

So, Hello guys today i will shared you about how to create a programme for complex nimber using  c programming;
So, Without any click bait Let's start 
Here is the source code for Complex Number Coding:

  •  #include <stdio.h>
  • #include <stdlib.h>
  • struct complex
  • {
  •   double real, imag;       //real and imag of type double
  • };
  • int main()
  • {
  •   int choice, a, b, z;
  •   struct complex x, y, c;
  •   while(1)
  •   {
  •   printf("Enter your choice\n");          //choices for operations
  •     printf("Press 1. Addition.\n");
  •     printf("Press 2. Subtra tion\n");
  •     printf("Press 3. Multiplication\n");
  •     printf("Press 4. Division\n");
  •     printf("Press 5. Exit\n");
  •     scanf("%d", &choice);
  •     if (choice == 5)
  •       exit(0);
  •     if (choice >= 1 && choice <= 4)
  •     {
  •       printf("Enter the value of x and y for first complex number:");
  •       scanf("%lf%lf", &x.real,&x.imag);
  •       printf("Enter the value of x and y for second complex number:");
  •       scanf("%lf%lf", &y.real,&y.imag);
  •     }
  •     if (choice == 1)                           //addition
  •     {
  •       c.real = x.real + y.real;
  •       c.imag = x.imag + y.imag;

  •       if (c.imag >= 0)
  •         printf("Sum of the complex numbers = %.2lf+%.2lfi", c.real, c.imag);
  •       else
  •         printf("Sum of the complex numbers = %.2lf %.2lfi", c.real, c.imag);
  •     }
  •     else if (choice == 2)                      //subtraction
  •     {
  •       c.real = x.real - y.real;
  •       c.imag = x.imag - y.imag;

  •       if (c.imag >= 0)
  •         printf("Difference of the complex numbers = %.2lf+%.2lfi", c.real, c.imag);
  •       else
  •         printf("Difference of the complex numbers = %.2lf %.2lfi", c.real, c.imag);
  •     }
  •     else if (choice == 3)                     //multiplication
  •     {
  •       c.real = x.real*y.real - x.imag*y.imag;
  •       c.imag = x.imag*y.real + x.real*y.imag;

  •       if (c.imag >= 0)
  •         printf("Multiplication of the complex numbers = %.2lf+%.2lfi", c.real, c.imag);
  •       else
  •         printf("Multiplication of the complex numbers = %.2lf %.2lfi", c.real, c.imag);
  •     }
  •     else if (choice == 4)                     //division
  •     {
  •       if (y.real == 0 && y.imag == 0)
  •         printf("Division by 0 + 0i isn't allowed.");
  •       else
  •       {
  •         a = x.real*y.real + x.imag*y.imag;
  •         b = x.imag*y.real - x.real*y.imag;
  •         z = y.real*y.real + y.imag*y.imag;

  •         if (a%z == 0 && b%z == 0)
  •         {
  •           if (b/z >= 0)
  •             printf("Division of the complex numbers = %.2lf+%.2lfi", a/z, b/z);
  •           else
  •             printf("Division of the complex numbers = %d %di", a/z, b/z);
  •         }
  •         else if (a%z == 0 && b%z != 0)
  •         {
  •           if (b/z >= 0)
  •             printf("Division of two complex numbers = %.2lf+ %.2lf/%.2lfi", a/z, b, z);
  •           else
  •             printf("Division of two complex numbers = %.2lf%.2lf/%.2lfi", a/z, b, z);
  •         }
  •         else if (a%z != 0 && b%z == 0)
  •         {
  •           if (b/z >= 0)
  •             printf("Division of two complex numbers = %.2lf/%.2lf+%.2lfi", a, z, b/z);
  •           else
  •             printf("Division of two complex numbers = %.2lf %.2lf/%.2lfi", a, z, b/z);
  •         }
  •         else
  •         {
  •           if (b/z >= 0)
  •             printf("Division of two complex numbers = %.2lf/%.2lf + %.2lf/%.2lfi",a, z, b, z);
  •           else
  •             printf("Division of two complex numbers = %.2lf/%.2lf %.2lf/%.2lfi", a, z, b, z);
  •         }
  •       }
  •     }
  •     else
  •       printf("Invalid choice.");

  •     printf("\nPress any key to enter choice again...\n");
  •   }
  • }
Output is here:




___________________________________________________________________________________

This Blog Is written by:
Suryanshsk(Software Developer,Programmer,Coder,youtuber)

___________________________________________________________________________________

Follow Me on

Instagram: @Suryanshsk

Youtube: Tech Suryanshsk


Spotify: @suryanshsk

Jiosaavn: @suryanshsk

Reeso: @suryanshsk

Comments

Popular posts from this blog

Hotel Billing Management Software By Suryanshsk

Develope Snake game using Html Code

Mother tongue of programming language (C language coding) -By Suryanshsk