Home » Ruby programming » Ruby programs

# 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

- 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

**Ad:**
Are you a blogger? Join our Blogging forum.

Learn PCB Designing: PCB DESIGNING TUTORIAL