# Ruby program to calculate the sum of all odd numbers up to N

Calculating the same of all odd numbers: Here, we are going to learn how to calculate the sum of all odd numbers up to N in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on August 21, 2019

## 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.

Methods used:

• puts: This is a predefined method and is used for writing strings on the console.
• gets: This method is used for taking input from the user in the form of string.
• .odd?: This is a predefined method in Ruby library which is used to find whether the specified number is odd or not.
• %: This is an arithmetic operator commonly known as the mod operator. It takes two arguments and returns the remainder.

Variables used:

• sum: This variable is used to store the sum of all odd numbers. It is initialized by 0.
• n: This is storing the limit till which the loop will run.
• i: This is a loop variable which is initialized by 1.

### 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:

```=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.

TOP Interview Coding Problems/Challenges

Learn PCB Designing: PCB DESIGNING TUTORIAL

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.