Q:

Ruby program to create Complex numbers from given imaginary parts

belongs to collection: Ruby Basic Programs

0

In this program, we will create variables with some initial values. Then we will convert given numbers into a complex number using the i() function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to create a Complex number from a given imaginary part is given below. The given program is compiled and executed successfully.

# Ruby program to create Complex number 
# from given imaginary part

num1 =  10;
num2 = -20;
num3 =  30;

print "Num1: "  ,num1.i();
print "\nNum2: ",num2.i();
print "\nNum3: ",num3.i();

Output:

Num1: 0+10i
Num2: 0-20i
Num3: 0+30i

Explanation:

In the above program, we created three variables num1num2num3 initialized with 10, -20, 30 respectively. Then we created complex numbers from given numbers where given numbers were used as imaginary parts using the i() function. After that, we printed the result.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to demonstrate the floor() function... >>
<< Ruby program to get the imaginary part of the give...