Logo

Programming-Idioms

History of Idiom 76 > diff from v60 to v61

Edit summary for version 61 by programming-idioms.org:
[Elixir] Revert code, fix URLs

Version 60

2019-09-28, 16:49:33

Version 61

2019-10-14, 11:55:36

Idiom #76 Binary digits from an integer

Create the string s of integer x written in base 2.

E.g. 13 -> "1101"

Idiom #76 Binary digits from an integer

Create the string s of integer x written in base 2.

E.g. 13 -> "1101"

Extra Keywords
int radix
Extra Keywords
int radix
Imports
  Integer.to_string(x, 2)
Imports
Code
#include<stdio.h>

int toBinary(int decimalNo);                //convert a integer number in base 10 to binary
int main(void){

 int num;
 scanf("%d",&num);
 int num_converted = toBinary(num); 

 printf("The number %d converted to binary is %d", num, num_converted);
}

int toBinary(int decimalNo){

//Base case
 if(decimalNo < 2) 
   return decimalNo;

//Recursive case
  return toBinary(decimalNo / 2) * 10 + decimalNo % 2;
 
 
}
Code
s = Integer.to_string(x, 2)
Doc URL
http://elixir-lang.org/docs/v1.0/elixir/Integer.html#to_string/2
Doc URL
https://hexdocs.pm/elixir/master/Integer.html#to_string/2
Demo URL
http://play.elixirbyexample.com/s/d2e09a39c8