0% found this document useful (0 votes)
3 views

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)
3 views

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
You are on page 1/ 3

import java.util.

*;

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=s1.real+ s2.real;
i=s1.img +s2.img;
System.out.println(r+"+"+i+"i");
}

void subtraction(Complex s1,Complex s2)


{
float r,i;
r=s1.real- s2.real;
i=s1.img -s2.img;
System.out.println(r+"+"+"("+i+")"+"i");
}

void multiplication(Complex s1,Complex s2)


{
float r,i;
r=s1.real*s2.real-s1.img*s2.img;
i=s1.real*s2.img +s1.img*s2.real;
System.out.println(r+"+"+i+"i");
}
void division(Complex s1,Complex s2)
{
float r,i;
r=(s1.real*s2.real+ s1.img*s2.img)/
(s2.real*s2.real+s2.img*s2.img);
i=(s1.img*s2.real -s1.real*s2.img)/
(s2.real*s2.real+s2.img*s2.img);
System.out.println(r+"+"+"("+i+")"+"i");
}
}

public class arithmetic


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

Scanner sc=new Scanner(System.in);


r=sc.nextFloat();
i=sc.nextFloat();
Complex c1=new Complex(r,i);

System.out.println("Enter real and imaginary part of


2nd complex number: ");
r=sc.nextFloat();
i=sc.nextFloat();
Complex c2=new Complex(r,i);
Complex c3=new Complex();

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

int ch;
ch=sc.nextInt();
switch(ch)
{
case 1:
c3.addition(c1,c2);
break;
case 2:
c3.subtraction(c1,c2);
break;
case 3:
c3.multiplication(c1,c2);
break;
case 4:
c3.division(c1,c2);
break;
case 5:
System.exit(0);
default:
System.out.println("Enter proper choice");

You might also like