Categories
Uncategorised

Structures – Complex Numbers

/ Copyright (C) 2007 Ankur Banerjee. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. http://www.fsf.org/licensing/licenses/lgpl.html */

/ Structures – Program to add two complex numbers /

#include <iostream.h>
#include <conio.h>

struct number
{
float re, im;
} num1, num2, result;

number& calc (number& n1, number& n2)
{
result.re = n1.re + n2.re;
result.im = n1.im + n2.im;
return result;
}

void main()
{
clrscr();
cout<<“Enter the real part of first complex number : “;
cin>>num1.re;
cout<<“Enter the imaginary part of first complex number : “;
cin>>num1.im;
cout<<“Enter the real part of second complex number : “;
cin>>num2.re;
cout<<“Enter the imaginary part of second complex number : “;
cin>>num2.im;
result = calc (num1, num2);
cout<<“The complex number result is “<<result.re<<” + “<<result.im
<<“i”<<endl;
getch();
}

/ Output */

Enter the real part of first complex number : 2
Enter the imaginary part of first complex number : 6
Enter the real part of second complex number : 8
Enter the imaginary part of second complex number : 9
The complex number result is 10 + 15i

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.