Home »
Ruby »
Ruby Programs
Ruby program to create Complex numbers from given imaginary parts
Ruby Example: Write a program to create Complex numbers from given imaginary parts.
Submitted by Nidhi, on December 11, 2021
Problem Solution:
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.
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 num1, num2, num3 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.
Ruby Basic Programs »