Ruby program to print sum of n natural numbers


Following program shows you how to print sum of n natural numbers.
In this program we get number from user and shows sum of n natural numbers using following formula
Sum of n natural numbers = input X (input + 1)/2

 puts "Enter a number:"
 input = Integer(gets.chomp)
 result = input * (input + 1) / 2
 puts result

Output:

Enter a number:
4
10