Write a Ruby program to check whether the sum of all the 3's of a given array of integers is exactly 9.
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
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer