I'm trying to write a code that will allow the user to input a number in binary (with a limit of 8 digits) using modulus. I have this so far:
import java.util.Scanner; public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a Binary Number (8 digit limit)");
int binaryNum = input.nextInt();
int m = 0;
int l;
for (int n = 0; n <= 7; n++){
l = (2^n);
if(binaryNum%(10^(n+1)) == (10^n)){
m = m + l;
binaryNum = binaryNum - (10^n);
}
}
System.out.println("Decimal: " + m);
}
}
I tried inputting the number 1001010, but whenever I did, it came out as "Decimal: 2" which is clearly wrong. Can someone help?