Q1. Write a program to print all binary representation for integer a, within the range (m, n). consecutive 1s in it. such that the binary representation of ai does not contain two or more.
Read the input from STDIN and write the output to STDOUT. You should not write arbitrary strings while reading the input and while printing as these T the standard output. 16519
Constraints:
II) There would be at least one integer between m and n whose binary representation will not have 2 or more continues 1โs
4 Input Format:
Input has two integers m and n. separated by white space.
Output Format
The output contains binary representations of all integers which do not contain two or more consecutive 1s in it Integers are printed in separate lines
Sample Input 1:
511
Sample Output 1:
1000
1001
1010
Coding Answers :
def no_consecutive_ones(n: int) -> bool:
bin_n = bin(n)[2:]
for i in range(len(bin_n)-1):
if bin_n[i] == โ1โ and bin_n[i+1] == โ1โ:
- return False
return Truem, n = map(int, input().split())
for i in range(m+1, n):
if no_consecutive_ones(i):
print(bin(i)[2:])
Python Code
Telegram โ t.me/Kingocampusdrive
ย
Q2.
Coding Answers :
- import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int start = sc.nextInt(); // @Kingocampusdrive read in the start of the range
int end = sc.nextInt(); // read in the end of the range
// loop through the range of numbers
for (int i = start; i <= end; i++) {
System.out.println(Integer.toBinaryString(i)); // print the binary representation of the number
}
}
}
Java code