当前位置: 首页 > 工具软件 > mod_ruby > 使用案例 >

ruby判断奇数偶数_Ruby程序可计算所有不超过N的奇数之和

庄萧迟
2023-12-01

ruby判断奇数偶数

计算所有奇数相同 (Calculating the same of all odd numbers)

To calculate the sum of all odd 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 odd or not and then if it is found odd, the instruction to calculate sum should be processed. The logic requires very few lines of code. For checking whether the number is odd or not, you can either use .odd? method or % (mod) operator.

计算不超过N的所有奇数之和 ,首先,我们必须要求用户输入限制或N。 然后在第二步中,需要检查数字是否为奇数,然后如果发现为奇数,则应处理计算总和的指令。 逻辑需要很少的代码行。 要检查数字是否为奇数,可以使用.odd?。 方法或% (mod)运算符。

Methods used:

使用的方法:

  • puts: This is a predefined method and is used for writing strings on the console.

    puts :这是一种预定义的方法,用于在控制台上编写字符串。

  • gets: This method is used for taking input from the user in the form of string.

    gets :此方法用于从用户那里获取字符串形式的输入。

  • .odd?: This is a predefined method in Ruby library which is used to find whether the specified number is odd or not.

    。奇? :这是Ruby库中的预定义方法,用于查找指定数字是否为奇数。

  • %: This is an arithmetic operator commonly known as the mod operator. It takes two arguments and returns the remainder.

    % :这是一个算术运算符,通常称为mod运算符。 它接受两个参数并返回其余的参数。

Variables used:

使用的变量:

  • sum: This variable is used to store the sum of all odd numbers. It is initialized by 0.

    sum :此变量用于存储所有奇数的和。 由0初始化。

  • n: This is storing the limit till which the loop will run.

    n :这存储循环将要运行的极限。

  • i: This is a loop variable which is initialized by 1.

    i :这是一个由1初始化的循环变量。

Ruby代码计算所有不超过N的奇数之和 (Ruby code to calculate the sum of all odd numbers up to N)

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

sum=0

puts "Enter n: "
n=gets.chomp.to_i
i=1
while(i<=n)
	if(i%2!=0)
		sum=sum+i
		i=i+1
	else
		i=i+1
	end
end

puts "The sum is #{sum}"

Output

输出量

RUN 1:
Enter n:
10
The sum is 25

RUN 2:
Enter n:
5
The sum is 9

Method 2:

方法2:

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

sum=0

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

i=1
while(i<=n)
	if(i.odd?)
		sum=sum+i
		i=i+1
	else
		i=i+1
	end
end

puts "The sum is #{sum}"

Output

输出量

Enter n:
12
The sum is 36

Code explanation:

代码说明:

In both the methods, first, we are taking input from the user. That input is facilitating help in letting us know till where we have to find the odd numbers. We are using while loop to process all integers from 1 to n. Inside the loop, we are checking the number using % operator or .odd? Method. If the result comes out to be true then we are adding that integer to the sum variable which is initialized with 0. The loop will be terminated when I will become equal to n. Eventually, we will have the sum and we are printing it on the console with the help of puts method.

在这两种方法中,首先,我们从用户那里获取输入。 该输入有助于使我们知道必须找到奇数的位置。 我们正在使用while循环来处理从1到n的所有整数。 在循环内部,我们使用%运算符或.odd?检查数字。 方法。 如果结果为真,则我们将该整数添加到以0初始化的sum变量中。当我等于n时,循环将终止。 最终,我们将获得总和,并借助puts方法将其打印在控制台上。

翻译自: https://www.includehelp.com/ruby/calculate-the-sum-of-all-odd-numbers-up-to-n.aspx

ruby判断奇数偶数

 类似资料: