Q:

Write a Ruby program to check whether the sum of all the 3's of a given array of integers is exactly 9

0

Write a Ruby program to check whether the sum of all the 3's of a given array of integers is exactly 9.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

def check_array(nums)
   sum = 0
   i = 0
   while i < nums.length
       if(nums[i] == 3)
			sum += 3
       end
    i += 1
   	end
   return (sum == 9);
end
print check_array([3, 5, 3, 3]),"\n"
print check_array([3, 3, 2, 1]),"\n"
print check_array([3, 3, 3, 3]),"\n"

Output:

true
false
false

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now