Slip27
Write an R Program to calculate binary into Decimal of a given number.
convert_to_binary = function(n)
{
if(n > 1)
{
convert_to_binary(as.integer(n/2))
}
cat(n %% 2)
}
decimal = 12
print("Decimal number")
print(decimal)
print("Binary number")
binary = convert_to_binary(decimal)
Output:-
> convert_to_binary = function(n)
+ {
+ if(n > 1)
+ {
+ convert_to_binary(as.integer(n/2))
+ }
+ cat(n %% 2)
+ }
> decimal = 12
> print("Decimal number")
[1] "Decimal number"
> print(decimal)
[1] 12
> print("Binary number")
[1] "Binary number"
> binary = convert_to_binary(decimal)
1100>