History of Idiom 76 > diff from v50 to v51
Edit summary for version 51 by haa:
[Elixir] covert
[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 radixExtra Keywords
int radixCode
#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;
}