Write a Ruby program to check two integers and return whichever value is nearest to the value 10

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

Write a Ruby program to check two integers and return whichever value is nearest to the value 10, or return 0 if two integers are equal.

الأجوبة

def text_int(a, b)
    ma = (10-a).abs;
	mb = (10-b).abs;
	if (ma < mb)
		return a;
	end
	if (mb < ma)
		return b;
	end
	return 0;
end
print text_int(7, 14),"\n"
print text_int(6, 9),"\n"
print text_int(5, 5)
Output:
7
9
0
هل كان المحتوى مفيد؟

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

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