Google PlusFacebookTwitter

Structures – Complex Numbers

By on Mar 25, 2007 in On A Whim | 0 comments

Share On GoogleShare On FacebookShare On Twitter

/* 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



Submit a Comment

Your email address will not be published. Required fields are marked *