WHAT'S NEW?
Loading...

Code for sum of complex numbers using structure


#include<iostream.h>

struct complex
{
    int x, y;
};

void main()
{
    complex c1,c2,sum;
    complex add (complex, complex);      //function prototype with returning value and arugment as structure
    cout<<"\n Enter the real part of the first complex number";
    cin>>c1.x;
    cout<<"\n Enter the imaginary part of the first complex number";
    cin>>c1.y;
    cout<<"\n Enter the real part of the second complex number";
    cin>>c2.x;
    cout<<"\n Enter the imaginary part of the second complex number";
    cin>>c2.y;
    sum=add(c1, c2);              //function calling
    cout<<"\n The sum of the complex number  is : ";
    if(sum.y>0||sum.y==0)
        cout<<sum.x<<"+"<<sum.y<<"i";
    else
        cout<<sum.x<<sum.y<<"i";
}

complex add(complex c1, complex c2)
{
    complex sum1;
    sum1.x=0;
    sum1.y=0;
    sum1.x=c1.x+c2.x;
    sum1.y=c1.y+c2.y;
    return(sum1);
}

0 comments:

Post a Comment