Saturday, October 21, 2017

Program to Implement Addition, Subtraction, Multiplication and Division of Complex Number in C

/*==========================================================
**
**  File Name     : complex_no.c
**  Creation Date : Sat 21 Oct 2017 08:58:51 PM IST
**  Last Modified : Sat 21 Oct 2017 10:44:13 PM IST
**  Compiler      : gcc
**  Author        : Manoj Kumar Patra, Asst. Professor
**  Organization  : Vignan University, Guntur, India.
**
**==========================================================*/

#include<stdio.h>
#include<stdlib.h>
struct complex
{
    int real;
    int img;
};

void add(struct complex n1, struct complex n2);
void sub(struct complex n1, struct complex n2);
void mul(struct complex n1, struct complex n2);
void division(struct complex n1, struct complex n2);

int main()
{
    printf("\n= = = = Arithemetic Operations on Complex number in C = = = =\n\n");
    int choice;
    struct complex n1, n2;
    printf("Enter the real part of first complex number: ");
    scanf("%d",&n1.real);
    printf("Enter the imag part of first complex number: ");
    scanf("%d",&n1.img);
    printf("Enter the real part of second complex number: ");
    scanf("%d",&n2.real);
    printf("Enter the imag part of second complex number: ");
    scanf("%d",&n2.img);
    printf("= - - - - - - - - - - - - - - - - - - - - - - - - - - - - - =\n");
    if(n1.img>=0)
            printf("First complex number is = %d+%di\n", n1.real, n1.img);
    else
            printf("First complex number is = %d%di\n", n1.real, n1.img);
    if(n2.img>=0)
            printf("Second complex number is = %d+%di\n", n2.real, n2.img);
    else
            printf("Second complex number is = %d%di\n", n2.real, n2.img);
    printf("= - - - - - - - - - - - - - - - - - - - - - - - - - - - - - =\n");

    while(1)
    {        
        printf("\n1. Addition\n");
        printf("2. Subtraction\n");
        printf("3. Multiplication\n");
        printf("4. Division\n");
        printf("5. Quit\n");
        printf("Enter your choice (1-5): ") ;
        scanf("%d", &choice);

        switch(choice)
        {
            case 1:
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                add(n1,n2);
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 2:
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                sub(n1,n2);
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 3:
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                mul(n1,n2);
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 4:
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                division(n1,n2);
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
                break;
            case 5:
                printf("Program Terminated\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
                return;
            default :
                printf("Oops.. Wrong choice! Select Again\n");
                printf("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n");
        }
    }
}

void add(struct complex n1, struct complex n2)
{
    struct complex res;
    res.real = n1.real+n2.real;
    res.img = n1.img+n2.img;
    if(res.img>=0)
            printf("Addition of two complex numbers is = %d+%di\n", res.real, res.img);
    else
            printf("Addition of two complex numbers is = %d%di\n", res.real, res.img);
}

void sub(struct complex n1, struct complex n2)
{
    struct complex res;
    res.real = n1.real-n2.real;
    res.img = n1.img-n2.img;
    if(res.img>=0)
            printf("Subtraction of two complex numbers is = %d+%di\n", res.real, res.img);
    else
            printf("Subtraction of two complex numbers is = %d%di\n", res.real, res.img);
}

void mul(struct complex n1, struct complex n2)
{
    struct complex res;
    res.real = n1.real*n2.real-n1.img*n2.img;
    res.img = n1.real*n2.img+n1.img*n2.real;
    if(res.img>=0)
            printf("Multiplication of two complex numbers is = %d+%di\n", res.real, res.img);
    else
            printf("Multiplication of two complex numbers is = %d%di\n", res.real, res.img);
}

void division(struct complex n1, struct complex n2)
{
    struct complex res;
    res.real = (n1.real*n2.real + n1.img*n2.img)/(n2.real*n2.real+n2.img*n2.img);
    res.img = (n1.img*n2.real - n1.real*n2.img)/(n2.real*n2.real + n2.img*n2.img);
    if(res.img>=0)
            printf("Division of two complex numbers is = %d+%di\n", res.real, res.img);
    else
            printf("Division of two complex numbers is = %d%di\n", res.real, res.img);
}

OutPut:

manoj@ubuntu:~$ gcc complex_no.c -o complex_no
manoj@ubuntu:~$ ./complex_no

= = = = Arithemetic Operations on Complex number in C = = = =

Enter the real part of first complex number: 4
Enter the imag part of first complex number: 6
Enter the real part of second complex number: 2
Enter the imag part of second complex number: 3
= - - - - - - - - - - - - - - - - - - - - - - - - - - - - - =
First complex number is = 4+6i
Second complex number is = 2+3i
= - - - - - - - - - - - - - - - - - - - - - - - - - - - - - =

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit
Enter your choice (1-5): 1
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Addition of two complex numbers is = 6+9i
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit
Enter your choice (1-5): 2
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Subtraction of two complex numbers is = 2+3i
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit
Enter your choice (1-5): 3
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Multiplication of two complex numbers is = -10+24i
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit
Enter your choice (1-5): 4
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Division of two complex numbers is = 2+0i
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit
Enter your choice (1-5): 5
Program Terminated
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

manoj@ubuntu:~$

1 comment:

  1. αγγελιες εργασιας

    Το MAXMAG είναι το ηλεκτρονικό περιοδικό που συνδυάζει υπεύθυνες πληροφορίες με ψυχαγωγία, αγάπη για το κλασικό, αγγέλιες εργασίας και την ανάγκη για καινοτομία.

    ReplyDelete