@kasturichakraborty36_24075fa79f00d4e8 Sorry for the late reply. The problems with your code is :
i) When you have initialized dec_value it should be initialized like this :
int dec_value = 0;
not like this
int dec_value += 0;
ii) Take input from user dont hard code the value of int num in main function.
iii)Major problem : The code you have sent contains two main functions which is not allowed so change the upper main funtion to binaryToDecimal and change its return type to int.
Corrected code is below :
import java.util.*;
public class Main {
public static int binaryToDecimal(int n) {
int num = n;
int dec_value = 0;
int base = 1;
int temp = num;
while (temp > 0) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println(binaryToDecimal(num));
}
}