C++

Convert Decimal to Binary in C++ and Ruby

C++

void binary(int number) {
  int remainder;
 
  if(number <= 1) {
    cout << number;
    return;
  }
 
  remainder = number%2;
  binary(number >> 1);
  cout << remainder;
}

Ruby

>> 7403.to_s(2)
=>1110011101011

ใช้ภาษา ruby สบายใจกว่ากันเยอะ bug ที่จะเกิดน้อยกว่าภาษา C++ แน่ๆ (พูดให้คนอ่านเปลี่ยนมาเล่น ruby กันมากขึ้น)

0
Your rating: None