Write simple Ruby programs that uses arrays in Ruby.
arrays.rb
print("Enter size:\n")
n = gets.to_i
arr=[]
print("Enter #{n} elements:\n")
for i in 0..n-1
arr.push(gets.to_i)
end
sum=0
for i in 0..n-1
sum=sum+arr[i]
end
print("Sum of array elements is #{sum}")
INPUT
Enter size:
4
Enter 4 elements:
1
22
5
2
OUTPUT
Sum of array elements is 30
arrays.rb
print("Enter size:\n")
n = gets.to_i
arr=[]
print("Enter #{n} elements:\n")
for i in 0..n-1
arr.push(gets.to_i)
end
sum=0
for i in 0..n-1
sum=sum+arr[i]
end
print("Sum of array elements is #{sum}")
INPUT
Enter size:
4
Enter 4 elements:
1
22
5
2
OUTPUT
Sum of array elements is 30