public class ComplexNumber{ //for real and imaginary parts of complex numbers double real, img; //constructor to initialize the complex number ComplexNumber(double r, double i){ this.real = r; this.img = i; } public static ComplexNumber sum(ComplexNumber c1, ComplexNumber c2) { //creating a temporary complex number to hold the sum of two numbers ComplexNumber temp = new ComplexNumber(0, 0); temp.real = c1.real + c2.real; temp.img = c1.img + c2.img; //returning the output complex number return temp; } public static void main(String args[]) { ComplexNumber c1 = new ComplexNumber(5.5, 4); ComplexNumber c2 = new ComplexNumber(1.2, 3.5); ComplexNumber temp = sum(c1, c2); System.out.printf("Sum is: "+ temp.real+" + "+ temp.img +"i"); } } |
JAVA File IO examples
JAVA Date & Time Progamming Examples
JAVA Collections
var = "Python Programming"What is the output of print var?
Ruby program to add two integer numbers
how to create an array with Array.[](*args) in Ruby ?
What are the various Ruby runtimes, and how are they different?
Ruby program to check whether the given number is prime or not
Ruby program to reverse a string
Ruby program to check whether the given number is palindrome
Ruby program to print Fibonacci series
How to Replace array elements in Ruby?
Ruby program to print an array
Ruby program to check whether the given number is Armstrong
Program to Print Triangle of Numbers in Ruby
How to add/remove elements to Array in Ruby?
How to shuffle an array in Ruby?
Creating Array with Array.new(size, obj) in Ruby
Ruby program to generate random numbers
Ruby program to Calculate the factorial of given number
What are #method_missing and #send? Why are they useful?
How to Sort Array in Ruby?
How to get index of array element in Ruby
How to Get Input with Gets in Ruby
How to create two dimensional array in ruby?