Logo

Programming-Idioms

History of Idiom 76 > diff from v50 to v51

Edit summary for version 51 by haa:
[Elixir] covert

Version 50

2019-06-20, 06:13:35

Version 51

2019-06-20, 06:20:28

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
Imports
  Integer.to_string(x, 2)
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
#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;
 
 
}
Doc URL
http://elixir-lang.org/docs/v1.0/elixir/Integer.html#to_string/2
Doc URL
http://elixir-lang.org/docs/v1.0/elixir/Integer.html#to_string/2
Demo URL
http://play.elixirbyexample.com/s/d2e09a39c8
Demo URL
http://play.elixirbyexample.com/s/d2e09a39c8