ruby判断奇数偶数_Ruby程序计算所有偶数之和

ruby判断奇数偶数

在Ruby中计算偶数之和 (Calculating sum of even numbers in Ruby)

For calculating the sum of all even numbers up to n, first, we have to ask the user to enter the limit or n. Then in the second step, it is required to check whether the number is even or not and then if it is found to be even, the sum should be calculated. The logic mentioned can be implemented in a few lines of code. For checking whether the number is even or not, you can either use .even? method or %(mod) operator.

为了计算不超过n的所有偶数之和 ,首先,我们必须要求用户输入限制或n 。 然后,在第二步中,需要检查数字是否为偶数,然后如果发现为偶数,则应计算总和。 所提到的逻辑可以用几行代码来实现。 要检查数字是否为偶数,可以使用.even? 方法或%(mod)运算符。

Methods used:

使用的方法:

  • puts: This is used to put a string on the console.

    puts :用于在控制台上放置字符串。

  • gets: The method is used for taking input from the user.

    gets :该方法用于接收用户的输入。

  • %: This is the mod operator. It is an arithmetic operator which takes two values as an argument and returns the remainder.

    :这是mod运算符。 它是一个算术运算符,它使用两个值作为参数并返回余数。

  • .even?: This is a predefined method used to verify even number.

    。甚至? :这是用于验证偶数的预定义方法。

Ruby代码计算所有偶数之和 (Ruby code to calculate the sum of all even numbers)

Method 1 : Using the classical way using %2

方法1:使用%2的经典方式

=begin
Ruby program to calculate the sum of 
all even numbers upto n
=end

sum=0

puts "Enter n:-"
n=gets.chomp.to_i

i=1
while(i<=n)
	if(i%2==0) 	#using % operator
		sum=sum+i
		i=i+1
	else
		i=i+1
	end
end

puts "The sum is #{sum}"

Output

输出量

RUN 1:
Enter n:-
5
The sum is 6

RUN 2:
Enter n:-
60
The sum is 930

Explanation:

说明:

In this program, we will calculate the sum of all even numbers up to n. We have used the remainder operator for this %. As this is division by two, then the number will return only 0 or 1 which are directly test in the if condition. n is the number up to which the check is to be performed. The variable Sum stores the sum of all the even numbers. Which is returned by the program after all numbers till n are traversed using a loop (while loop in our program).

在此程序中,我们将计算所有偶数之和,直到n 。 我们已经为这个%使用了余数运算符。 由于它被二除,因此该数字将仅返回0或1,这些条件直接在if条件下进行测试。 n是要进行检查的编号。 变量Sum存储所有偶数之和。 使用循环(在程序中为while循环 )遍历直到n为止的所有数字之后,程序将返回该值。

Method 2: Using the even method that returns true if the number is even

方法2:使用偶数方法,如果数字为偶数,则返回true

=begin
Ruby program to calculate the sum of 
all even numbers upto n
=end

sum=0

puts "Enter n:-"
n=gets.chomp.to_i

i=1
while(i<=n)
	if(i.even?) #using .even? method
		sum=sum+i
		i=i+1
	else
		i=i+1
	end
end

puts "The sum is #{sum}"

Output

输出量

Enter n:-
10
The sum is 30

Explanation:

说明:

In this program, we will calculate the sum of all even numbers up to n. We have used a Ruby method named even? to check if the number is even or not. The method returns true if the number is even. n is the number up to which the check is to be performed. The variable sum stores the sum of all the even numbers. Which is returned by the program after all numbers till n are traversed using a loop (while loop in our program).

在此程序中,我们将计算所有偶数之和,直到n 。 我们使用了一个名为偶数的Ruby方法? 检查数字是否为偶数。 如果数字为偶数,则该方法返回true。 n是要进行检查的编号。 变量sum存储所有偶数之和。 使用循环(在程序中为while循环 )遍历直到n为止的所有数字之后,程序将返回该值。

翻译自: https://www.includehelp.com/ruby/calculate-the-sum-of-all-even-numbers.aspx

ruby判断奇数偶数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值