×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to create Complex numbers from given imaginary parts

Last Updated : December 15, 2025

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 »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.