0% found this document useful (0 votes)
12 views3 pages

Assignment 1

The document contains a Java program that defines a Complex class for performing arithmetic operations (addition, subtraction, multiplication, and division) on complex numbers. It includes methods for each operation and a main class that takes user input for two complex numbers and allows the user to choose an operation to perform. The program runs in a loop until the user decides to exit.

Uploaded by

4vxwnr5s6v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Assignment 1

The document contains a Java program that defines a Complex class for performing arithmetic operations (addition, subtraction, multiplication, and division) on complex numbers. It includes methods for each operation and a main class that takes user input for two complex numbers and allows the user to choose an operation to perform. The program runs in a loop until the user decides to exit.

Uploaded by

4vxwnr5s6v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

*;

class Complex
{
float real;
float img;

Complex()
{
real=0; // default constructor
img=0;
}

Complex(float r,float i)
{
real=r;
img=i;
}

void addition(Complex s1,Complex s2)


{
float r,i;
r=[Link]+ [Link];
i=[Link] +[Link];
[Link](r+"+"+i+"i");
}

void subtraction(Complex s1,Complex s2)


{
float r,i;
r=[Link]- [Link];
i=[Link] -[Link];
[Link](r+"+"+"("+i+")"+"i");
}

void multiplication(Complex s1,Complex s2)


{
float r,i;
r=[Link]*[Link]*[Link];
i=[Link]*[Link] +[Link]*[Link];
[Link](r+"+"+i+"i");
}
void division(Complex s1,Complex s2)
{
float r,i;
r=([Link]*[Link]+ [Link]*[Link])/
([Link]*[Link]+[Link]*[Link]);
i=([Link]*[Link] -[Link]*[Link])/
([Link]*[Link]+[Link]*[Link]);
[Link](r+"+"+"("+i+")"+"i");
}
}

public class arithmetic


{
public static void main(String args[])
{
float r,i;
[Link]("Enter real and imaginary part of
1st complex number: ");

Scanner sc=new Scanner([Link]);


r=[Link]();
i=[Link]();
Complex c1=new Complex(r,i);

[Link]("Enter real and imaginary part of


2nd complex number: ");
r=[Link]();
i=[Link]();
Complex c2=new Complex(r,i);
Complex c3=new Complex();

while(true)
{
[Link](" Enter choice for\n 1. Addition
\n 2. Sub \n 3. Mult \n 4. Div \n 5. For exit");

int ch;
ch=[Link]();
switch(ch)
{
case 1:
[Link](c1,c2);
break;
case 2:
[Link](c1,c2);
break;
case 3:
[Link](c1,c2);
break;
case 4:
[Link](c1,c2);
break;
case 5:
[Link](0);
default:
[Link]("Enter proper choice");

You might also like