Home » Ruby programming » Ruby programs

# Ruby program to print power of a number

**Power of a number**: Here, we are going to learn **how to calculate/print power of a given number in Ruby programming language?**

Submitted by Hrithik Chandra Prasad, on August 16, 2019

## Power of a number in Ruby

The task to develop a **program that prints power of a number in Ruby programming language**.

If we want to calculate the power of a number manually then we have to multiply the base to itself by exponent times which means that if the base is **3** and the exponent is **4**, then power will be calculated as

**power = 3*3*3*3**, which will result in 81.

Let us put the above logic into codes. We have used two methods to calculate power, one is by using user-defined function “pow” and one is with the help of ** operator. If you want to write code from scratch then the first method is appropriate for you.

**Methods used:**

**puts**: This is used to create an interaction with the user by putting some message on the console.**gets**: This is used to take input from the user in the form of string.**to_i**: This method is used to convert any type into an integer type.**pow**: This is a user-defined function which takes two arguments and returns an integer value. It is defined purposefully for calculating power by taking base and exponent as an argument.

### Ruby code to calculate power of a number

=begin Ruby program to calculate power of a number. =end def pow(a,b) power=1 for i in 1..b power=power*a end return power end puts "Enter Base:-" base=gets.chomp.to_i puts "Enter exponent:-" expo=gets.chomp.to_i puts "The power is #{pow(base,expo)}"

**Output**

RUN 1: Enter Base:- 3 Enter exponent:- 5 The power is 243 RUN 2: Enter Base:- 2 Enter exponent:- 3 The power is 8

**Method 2:**

=begin Ruby program to calculate power of a number using ** operator. =end puts "Enter Base:-" base=gets.chomp.to_i puts "Enter exponent:-" expo=gets.chomp.to_i power=base**expo puts "The power is #{power}"

**Output**

RUN 1: Enter Base:- 5 Enter exponent:- 5 The power is 3125 RUN 2: Enter Base:- 9 Enter exponent:- 2 The power is 81

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions