Home »
Ruby »
Ruby Programs
Ruby program to get the real part from a Complex number
Ruby Example: Write a program to get the real part from a Complex number.
Submitted by Nidhi, on December 14, 2021
Problem Solution:
In this program, we will create 2 variables and initialize them with some values. Then we will get the real part from a complex number using the built-in real() function.
Program/Source Code:
The source code to get the real part from a Complex number is given below. The given program is compiled and executed successfully.
# Ruby program to get the real part
# from a Complex number
num1 = Complex(10, 23);
num2 = Complex(34, 45);
print "Real part num1: ",num1.real(),"\n";
print "Real part num2: ",num2.real(),"\n";
Output:
Real part num1: 10
Real part num2: 34
Explanation:
In the above program, we created two variables num1, num2 and initialized them with complex numbers. Then we used the real() function to get the real part from a complex number and printed the result.
Ruby Basic Programs »