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

  • برمجة
  • برمجة روبي

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
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...