Print the Sum of N numbers


Hai

And Sir gave a another task, huh.. It’s like print the Sun of N numbers.

# To print sum of N natural numbers
=begin
Program name: natural_numbers.rb
Date written: 18-12-2010
Date last modified: 18-12-2010
Author: Tha.Suresh
License: Free BSD
=end
# Main part of the program starts here
puts
print " Enter the number: "
number=gets.chomp.to_i
sum = 0
for iterator in 1..number do
sum = sum + iterator
end
puts
puts " Sum of N numbers is " + sum.to_s
# Main of the program ends here

Ex:

[root@mercury suresh_class]# ruby natural_numbers.rb

Enter the number: 9

Sum of N numbers is 45

😉

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s