Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*8.Write a program to count how many sequences of two equal bits ("00" or "11")
- * can be found in the binary representation of given integer number n*/
- import java.util.Scanner;
- public class CountOfEqualBitPairs {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.println("Enter a positive integer number!");
- int num =input.nextInt();
- int counter = 0;
- int duoCounts = 0;
- String binNum = Integer.toBinaryString(num);
- char[] binNumChar =binNum.toCharArray();
- for (int i = 0; i < binNumChar.length-1; i++) {
- if (binNumChar[i]==binNumChar[i+1]) {
- counter++;
- }
- if (counter ==1) {
- duoCounts++;
- counter = 0;
- }
- }
- System.out.println(duoCounts);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement