ruby打印_Ruby程序打印数字的力量
ruby打印
Ruby中数字的幂 (Power of a number in Ruby)
The task to develop a program that prints power of a number in Ruby programming language.
开发可以用Ruby编程语言打印数字幂的程序的任务。
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
如果要手动计算数字的幂,则必须将底数乘以自身乘以指数时间,这意味着如果底数为3且指数为4 ,则幂将计算为
power = 3*3*3*3, which will result in 81.
power = 3 * 3 * 3 * 3 ,结果为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.
让我们将以上逻辑放入代码中。 我们使用了两种方法来计算功率,一种方法是使用用户定义的函数“ pow”,另一种方法是借助**运算符。 如果您想从头开始编写代码,则第一种方法适合您。
Methods used:
使用的方法:
puts: This is used to create an interaction with the user by putting some message on the console.
puts :用于通过在控制台上放置一些消息来与用户进行交互。
gets: This is used to take input from the user in the form of string.
gets :用于接收用户以字符串形式的输入。
to_i: This method is used to convert any type into an integer type.
to_i :此方法用于将任何类型转换为整数类型。
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.
pow :这是一个用户定义的函数,它带有两个参数并返回一个整数值。 它的定义是有目的的,以底数和指数为参数来计算功效。
Ruby代码来计算数字的幂 (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:
方法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
翻译自: https://www.includehelp.com/ruby/print-power-of-a-number.aspx
ruby打印
ruby打印_Ruby程序打印数字的力量相关推荐
- ruby打印_Ruby程序打印一个数字的乘法表
ruby打印 打印乘法表 (Printing multiplication table) This requires a very simple logic where we only have to ...
- ruby 将字符串转为数组_Ruby程序将数组打印为字符串
ruby 将字符串转为数组 将数组打印为字符串 (Printing an array as string) Given an array and we have to print it as a st ...
- c语言++数组名【数字】_C ++程序在数组中打印所有非重复数字
c语言++数组名[数字] Problem statement: Write a C++ program to print all the non-repeated numbers in an arra ...
- C语言程序设计—循环设计 编写一个程序打印如下对称图形(行数由键盘输入1-9范围的值),例如下面是输入数字4时的情形:
编写一个程序打印如下对称图形(行数由键盘输入1-9范围的值),例如下面是输入数字4时的情形: 首先提示输入:"Enter n(1-9):\n" 输入格式:"%d" ...
- Java黑皮书课后题第5章:*5.18(使用循环语句打印4个图案)使用嵌套的循环语句,编写4个独立的程序打印下面的图案
*5.18(使用循环语句打印4个图案)使用嵌套的循环语句,编写4个独立的程序打印下面的图案 题目 题目概述 破题 代码 题目 题目概述 *5.18(使用循环语句打印4个图案)使用嵌套的循环语句,编写4 ...
- I00029 C语言程序-打印九九乘法表
这是小时候背的九九乘法表,是中文版,不是数字版,用C语言程序打印出来了. C语言程序如下: /* I00029 C语言程序-打印九九乘法表 */#include <stdio.h> #in ...
- python输出所有素数_Python程序打印一个区间内所有质数
Python程序打印一个区间内所有质数 在这个程序中,您将学习使用for循环打印一个区间内的所有质数并显示它. 要理解此示例,您应该了解以下Python编程主题: 一个大于1的正整数,除了1没有其他因 ...
- I00027 Java语言程序-打印九九乘法表
这是小时候背的九九乘法表,是中文版,不是数字版,用Java程序打印出来了. Java源程序如下: package com.elephant.multiply99table;public class M ...
- 青岛理工大学c语言软件,青岛理工大学C语言程序打印版.docx
课件程序 | 软件164王玉军编写 PAGE \* MERGEFORMAT1 课件程序打印版 1. 1+(1+2)++ #include int main() { int s,t,n; int i,j ...
最新文章
- 纠错工具之 - Proovread
- android 获取 service 信息
- 无线覆盖带机量比较大的型号推荐
- Struts2配置文件【代码库】
- java解析html的table
- python从csv读取数据到网页输入框_python中怎么把csv文件读取的数据显示在网页上...
- linux 大数字 进制转换,Linux下用bc快速进行数字进制转换
- java通过各种类型驱动连接数据库
- Nodejs基础:路径处理模块path总结
- python wechat flask_python3+flask不能解析消息
- 微信获取unionid
- OpenCore 启动菜单界面美化增加gui界面
- 终极解决电脑缺失dll,应用程序无法正常启动0xc000007b
- Codeforces Beta Round #94 (Div. 1 Only)A. Statues
- 第九周上机项目3:个人所得税计算器
- 推销员基础解法c++
- java 截取图片后缀
- 函数对象,lamdda表达式,function函数包装,std::bind函数绑定
- 行业研究-全球与中国移动现场服务解决方案市场现状及未来发展趋势
- 《互联网金融投资理财一册通》一一1.1 互联网金融
热门文章
- html5 ios cookie,ios – WKWebView,获取所有cookie
- IIS-ShortName-Scanner使用
- 前端生态混乱,AMPMIP在努力做标准化工作
- 玩转Jquery中的动画效果(animate方法)
- 深入css布局 (1) — 盒模型 元素分类
- mysql binlog空间维护
- ZOJ1081 Points Within
- Django集合Ueditor
- Spring-framework应用程序启动loadtime源码分析笔记(二)——@Transactional
- canvas 图片反色