I can run the code but it is showing run error

here the code:

import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
int snum = scn.nextInt();
int sb = scn.nextInt();
int res = binaryToDecimal(snum , sb);
System.out.println(res);

}
public static int binaryToDecimal(int snum , int sb) {
	int ans = 0;
	int multiplier = 1;
	while(snum != 0){
		int rem = snum % 10;
		ans = ans + (rem * multiplier);
		multiplier = multiplier * sb;
		snum = snum /10;
	}
	return ans;
}

}

Hey @sajidapalagiri

import java.util.Scanner;

public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
int snum = scn.nextInt();
// int sb = scn.nextInt();Why are you taking input sb
int res = binaryToDecimal(snum,2);
System.out.println(res);

}

public static int binaryToDecimal(int snum, int sb) {
	int ans = 0;
	int multiplier = 1;
	while (snum != 0) {
		int rem = snum % 10;
		ans = ans + (rem * multiplier);
		multiplier = multiplier * sb;
		snum = snum / 10;
	}
	return ans;
}

}

1 Like