Problem statement:- Program to convert binary to a decimal using recursion.
Data requirement:-
Input Data:- n
Output Data:-BinaryToDecimal(n)
Output Data:-BinaryToDecimal(n)
Program in C
Here is the source code of the C program using the recursive function for binary to decimal conversion.
Code:
#include<stdio.h>
int BinaryToDecimal(int n)
{
if(n==0)
return 0;
else
return (n% 10 + 2* BinaryToDecimal(n / 10));
}
int main()
{
int n;
printf("Enter the Binary Value:");
scanf("%d",&n);
printf("Decimal Value of Binary number is: %d",BinaryToDecimal(n));
}
Enter the Binary Value:1001
Decimal Value of Binary number is: 9
Program in C++
Here is the source code of the C++ program to convert binary to a decimal using recursive function.
Code:
#include<iostream>
using namespace std;
int BinaryToDecimal(int n)
{
if(n==0)
return 0;
else
return (n% 10 + 2* BinaryToDecimal(n / 10));
}
int main()
{
int n;
cout<<"Enter the Binary Value:";
cin>>n;
cout<<"Decimal Value of Binary number is: "<<BinaryToDecimal(n);
}
Enter the Binary Value:1000111
Decimal Value of Binary number is: 71
Program in Java
Here is the source code of the Java program to convert binary to a decimal using recursive function.
Code:
import java.util.Scanner;
public class BinaryToDecimalConvert {
static int BinaryToDecimal(int n)
{
if(n==0)
return 0;
else
return (n% 10 + 2* BinaryToDecimal(n / 10));
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int n;
System.out.print("Enter the Binary Value:");
n=cs.nextInt();
System.out.print("Decimal Value of Binary number is: "+BinaryToDecimal(n));
cs.close();
}
}
Enter the Binary Value:000111
Decimal Value of Binary number is: 7
Program in Python
Here is the source code of the Python program to convert binary to a decimal using recursive function.
Code:
def BinaryToDecimal(n):
if n==0:
return 0
else:
return (n% 10 + 2* BinaryToDecimal(n // 10))
n=int(input("Enter the Binary Value:"))
print("Decimal Value of Binary number is:",BinaryToDecimal(n))
Enter the Binary Value:001010101
Decimal Value of Binary number is: 85
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box