Answer:
public class Brainly
{
public static void main(String[] args)
{
BinaryConverter conv = new BinaryConverter();
String binStr = "01001101";
System.out.print(binStr + " in decimal is "+conv.BinToDec(binStr));
}
}
public class BinaryConverter
{
public int BinToDec(String binStr)
{
int d = 0;
while(binStr.length() > 0)
{
d = (d << 1) + ((binStr.charAt(0) == '1') ? 1: 0);
binStr = binStr.substring(1);
}
return d;
}
}
Explanation:
The program "eats" the string from left to right, and builds up the integer representation in variable "d" on the go. While there are digits left, it shifts the previous result to the left and sets the least signficant bit to 1 only if the corresponding string character is a 1.
Answer:
Explanation:
The code does not fail on the first step since 1900 divided by 4 is actually 475 and has no remainder, meaning that it should return True. The code won't work because the if statements need to be nested in a different format. The correct algorithm would be the following, which can also be seen in the picture attached below that if we input 1900 it would output is not a leap year as it fails on the division by 400 which gives a remainder of 0.75
input_year = int(input())
if input_year % 4 == 0:
if input_year % 100 == 0:
if input_year % 400 == 0:
print(input_year, "is a leap year")
else:
print(input_year, "- not a leap year")
else:
print(input_year, "is a leap year")
else:
print(input_year, "- not a leap year")