Sunday, 17 July 2011

Write a program to calculate the sum of n complex numbers using array of objects

import java.io.*;
class complexn25
{
int real;
int img;
public void input()
throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the real and imaginary part");
real=Integer.parseInt(br.readLine());
img=Integer.parseInt(br.readLine());
}

public void sum(complexn25 c[])
{
complexn25 s=new complexn25();
real=c[0].real;
img=c[0].img;
for(int i=1;i<c.length;i++)
{
real=real+c[i].real;
img=img+c[i].img;
}
System.out.println(real+"+"+img+"i");
}
}
class complexn25main
{
public static void main(String[] arg)
throws IOException
{
int n=0;
complexn25 p=new complexn25();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the number of complex numbers");
n=Integer.parseInt(br.readLine());
complexn25[] c=new complexn25[n];
for(int i=0;i<n;i++)
{
c[i]=new complexn25();
c[i].input();
}
complexn25 s=new complexn25();
s.sum(c);
}
}import java.io.*;
class complexn25
{
int real;
int img;
public void input()
throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the real and imaginary part");
real=Integer.parseInt(br.readLine());
img=Integer.parseInt(br.readLine());
}

public void sum(complexn25 c[])
{
complexn25 s=new complexn25();
real=c[0].real;
img=c[0].img;
for(int i=1;i<c.length;i++)
{
real=real+c[i].real;
img=img+c[i].img;
}
System.out.println(real+"+"+img+"i");
}
}
class complexn25main
{
public static void main(String[] arg)
throws IOException
{
int n=0;
complexn25 p=new complexn25();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the number of complex numbers");
n=Integer.parseInt(br.readLine());
complexn25[] c=new complexn25[n];
for(int i=0;i<n;i++)
{
c[i]=new complexn25();
c[i].input();
}
complexn25 s=new complexn25();
s.sum(c);
}
}

No comments:

Post a Comment

 

About The Author


I am a B.Tech Computer Science Student. I love Programming and blogging. I am Oracle certified Java Professional. Read More
Indian Blogger

Teachers Jobs

Footer 3

Twitter Facebook RSS YouTube Google
​

All Rights Reserved ©Sun Java Masters 2013