# Check if a number has bits in alternate pattern

Here given code implementation process.

``````// C program
// Check if a number has bits in alternate pattern
#include <stdio.h>

// Check that bit pair are different in given number or not
void alternatePattern(int num)
{
if (num <= 0)
{
return;
}
// Get given number
int n = num;
// Result indicator
int result = 1;
// Get last bit
int back = n & 1;
// left shift by one
n = n >> 1;
// Execute loop until
while (n > 0 && result == 1)
{
if ((n & 1) == back)
{
// When two consecutive bits are same
result = 0;
}
else
{
back = n & 1;
}
n = n >> 1;
}
// Display given number
printf("\n Number : %d", num);
if (result == 0)
{
printf("\n No");
}
else
{
printf("\n Yes");
}
}
int main(int argc, char
const *argv[])
{
int num = 85;
// 85 (1010101)
alternatePattern(num);
num = 42;
// 42 (101010)
alternatePattern(num);
num = 106;
// 106 (1101010)
alternatePattern(num);
num = 2;
// 2 (10)
alternatePattern(num);
num = 22;
// 22 (10110)
alternatePattern(num);
return 0;
}``````

#### Output

`````` Number : 85
Yes
Number : 42
Yes
Number : 106
No
Number : 2
Yes
Number : 22
No``````
``````/*
Java program
Check if a number has bits in alternate pattern
*/
public class Sequence
{
// Check that bit pair are different in given number or not
public void alternatePattern(int num)
{
if (num <= 0)
{
return;
}
// Get given number
int n = num;
// Result indicator
boolean result = true;
// Get last bit
int back = n & 1;
// left shift by one
n = n >> 1;
// Execute loop until
while (n > 0 && result == true)
{
if ((n & 1) == back)
{
// When two consecutive bits are same
result = false;
}
else
{
back = n & 1;
}
n = n >> 1;
}
// Display given number
System.out.print("\n Number : " + num );
if (result == false)
{
System.out.print("\n No");
}
else
{
System.out.print("\n Yes");
}
}

public static void main(String[] args)
{
int num = 85;
// 85 (1010101)
num = 42;
// 42 (101010)
num = 106;
// 106 (1101010)
num = 2;
// 2 (10)
num = 22;
// 22 (10110)
}
}``````

#### Output

`````` Number : 85
Yes
Number : 42
Yes
Number : 106
No
Number : 2
Yes
Number : 22
No``````
``````// Include namespace system
using System;
/*
C# program
Check if a number has bits in alternate pattern
*/
public class Sequence
{
// Check that bit pair are different in given number or not
public void alternatePattern(int num)
{
if (num <= 0)
{
return;
}
// Get given number
int n = num;
// Result indicator
int result = 1;
// Get last bit
int back = n & 1;
// left shift by one
n = n >> 1;
// Execute loop until
while (n > 0 && result == 1)
{
if ((n & 1) == back)
{
// When two consecutive bits are same
result = 0;
}
else
{
back = n & 1;
}
n = n >> 1;
}
// Display given number
Console.Write("\n Number : " + num);
if (result == 0)
{
Console.Write("\n No");
}
else
{
Console.Write("\n Yes");
}
}
public static void Main(String[] args)
{
int num = 85;
// 85 (1010101)
num = 42;
// 42 (101010)
num = 106;
// 106 (1101010)
num = 2;
// 2 (10)
num = 22;
// 22 (10110)
}
}``````

#### Output

`````` Number : 85
Yes
Number : 42
Yes
Number : 106
No
Number : 2
Yes
Number : 22
No``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ program
Check if a number has bits in alternate pattern
*/
class Sequence
{
public:
// Check that bit pair are different in given number or not
void alternatePattern(int num)
{
if (num <= 0)
{
return;
}
// Get given number
int n = num;
// Result indicator
int result = 1;
// Get last bit
int back = n &1;
// left shift by one
n = n >> 1;
// Execute loop until
while (n > 0 && result == 1)
{
if ((n &1) == back)
{
// When two consecutive bits are same
result = 0;
}
else
{
back = n &1;
}
n = n >> 1;
}
// Display given number
cout << "\n Number : " << num;
if (result == 0)
{
cout << "\n No";
}
else
{
cout << "\n Yes";
}
}
};
int main()
{
int num = 85;
// 85 (1010101)
num = 42;
// 42 (101010)
num = 106;
// 106 (1101010)
num = 2;
// 2 (10)
num = 22;
// 22 (10110)
return 0;
}``````

#### Output

`````` Number : 85
Yes
Number : 42
Yes
Number : 106
No
Number : 2
Yes
Number : 22
No``````
``````<?php
/*
Php program
Check if a number has bits in alternate pattern
*/
class Sequence
{
// Check that bit pair are different in given number or not
public	function alternatePattern(\$num)
{
if (\$num <= 0)
{
return;
}
// Get given number
\$n = \$num;
// Result indicator
\$result = 1;
// Get last bit
\$back = \$n & 1;
// left shift by one
\$n = \$n >> 1;
// Execute loop until
while (\$n > 0 && \$result == 1)
{
if ((\$n & 1) == \$back)
{
// When two consecutive bits are same
\$result = 0;
}
else
{
\$back = \$n & 1;
}
\$n = \$n >> 1;
}
// Display given number
echo "\n Number : ". \$num;
if (\$result == 0)
{
echo "\n No";
}
else
{
echo "\n Yes";
}
}
}

function main()
{
\$num = 85;
// 85 (1010101)
\$num = 42;
// 42 (101010)
\$num = 106;
// 106 (1101010)
\$num = 2;
// 2 (10)
\$num = 22;
// 22 (10110)
}
main();``````

#### Output

`````` Number : 85
Yes
Number : 42
Yes
Number : 106
No
Number : 2
Yes
Number : 22
No``````
``````/*
Node Js program
Check if a number has bits in alternate pattern
*/
class Sequence
{
// Check that bit pair are different in given number or not
alternatePattern(num)
{
if (num <= 0)
{
return;
}
// Get given number
var n = num;
// Result indicator
var result = 1;
// Get last bit
var back = n & 1;
// left shift by one
n = n >> 1;
// Execute loop until
while (n > 0 && result == 1)
{
if ((n & 1) == back)
{
// When two consecutive bits are same
result = 0;
}
else
{
back = n & 1;
}
n = n >> 1;
}
// Display given number
process.stdout.write("\n Number : " + num);
if (result == 0)
{
process.stdout.write("\n No");
}
else
{
process.stdout.write("\n Yes");
}
}
}

function main()
{
var num = 85;
// 85 (1010101)
num = 42;
// 42 (101010)
num = 106;
// 106 (1101010)
num = 2;
// 2 (10)
num = 22;
// 22 (10110)
}
main();``````

#### Output

`````` Number : 85
Yes
Number : 42
Yes
Number : 106
No
Number : 2
Yes
Number : 22
No``````
``````#   Python 3 program
#   Check if a number has bits in alternate pattern

class Sequence :
#  Check that bit pair are different in given number or not
def alternatePattern(self, num) :
if (num <= 0) :
return

#  Get given number
n = num
#  Result indicator
result = 1
#  Get last bit
back = n & 1
#  left shift by one
n = n >> 1
#  Execute loop until
while (n > 0 and result == 1) :
if ((n & 1) == back) :
#  When two consecutive bits are same
result = 0
else :
back = n & 1

n = n >> 1

#  Display given number
print("\n Number : ", num, end = "")
if (result == 0) :
print("\n No", end = "")
else :
print("\n Yes", end = "")

def main() :
num = 85
#  85 (1010101)
num = 42
#  42 (101010)
num = 106
#  106 (1101010)
num = 2
#  2 (10)
num = 22
#  22 (10110)

if __name__ == "__main__": main()``````

#### Output

`````` Number :  85
Yes
Number :  42
Yes
Number :  106
No
Number :  2
Yes
Number :  22
No``````
``````#   Ruby program
#   Check if a number has bits in alternate pattern

class Sequence
#  Check that bit pair are different in given number or not
def alternatePattern(num)
if (num <= 0)
return
end

#  Get given number
n = num
#  Result indicator
result = 1
#  Get last bit
back = n & 1
#  left shift by one
n = n >> 1
#  Execute loop until
while (n > 0 && result == 1)
if ((n & 1) == back)
#  When two consecutive bits are same
result = 0
else
back = n & 1
end

n = n >> 1
end

#  Display given number
print("\n Number : ", num)
if (result == 0)
print("\n No")
else
print("\n Yes")
end

end

end

def main()
num = 85
#  85 (1010101)
num = 42
#  42 (101010)
num = 106
#  106 (1101010)
num = 2
#  2 (10)
num = 22
#  22 (10110)
end

main()``````

#### Output

`````` Number : 85
Yes
Number : 42
Yes
Number : 106
No
Number : 2
Yes
Number : 22
No``````
``````/*
Scala program
Check if a number has bits in alternate pattern
*/
class Sequence
{
// Check that bit pair are different in given number or not
def alternatePattern(num: Int): Unit = {
if (num <= 0)
{
return;
}
// Get given number
var n: Int = num;
// Result indicator
var result: Int = 1;
// Get last bit
var back: Int = n & 1;
// left shift by one
n = n >> 1;
// Execute loop until
while (n > 0 && result == 1)
{
if ((n & 1) == back)
{
// When two consecutive bits are same
result = 0;
}
else
{
back = n & 1;
}
n = n >> 1;
}
// Display given number
print("\n Number : " + num);
if (result == 0)
{
print("\n No");
}
else
{
print("\n Yes");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Sequence = new Sequence();
var num: Int = 85;
// 85 (1010101)
num = 42;
// 42 (101010)
num = 106;
// 106 (1101010)
num = 2;
// 2 (10)
num = 22;
// 22 (10110)
}
}``````

#### Output

`````` Number : 85
Yes
Number : 42
Yes
Number : 106
No
Number : 2
Yes
Number : 22
No``````
``````/*
Swift 4 program
Check if a number has bits in alternate pattern
*/
class Sequence
{
// Check that bit pair are different in given number or not
func alternatePattern(_ num: Int)
{
if (num <= 0)
{
return;
}
// Get given number
var n: Int = num;
// Result indicator
var result: Int = 1;
// Get last bit
var back: Int = n & 1;
// left shift by one
n = n >> 1;
// Execute loop until
while (n > 0 && result == 1)
{
if ((n & 1) == back)
{
// When two consecutive bits are same
result = 0;
}
else
{
back = n & 1;
}
n = n >> 1;
}
// Display given number
print("\n Number : ", num, terminator: "");
if (result == 0)
{
print("\n No", terminator: "");
}
else
{
print("\n Yes", terminator: "");
}
}
}
func main()
{
var num: Int = 85;
// 85 (1010101)
num = 42;
// 42 (101010)
num = 106;
// 106 (1101010)
num = 2;
// 2 (10)
num = 22;
// 22 (10110)
}
main();``````

#### Output

`````` Number :  85
Yes
Number :  42
Yes
Number :  106
No
Number :  2
Yes
Number :  22
No``````
``````/*
Kotlin program
Check if a number has bits in alternate pattern
*/
class Sequence
{
// Check that bit pair are different in given number or not
fun alternatePattern(num: Int): Unit
{
if (num <= 0)
{
return;
}
// Get given number
var n: Int = num;
// Result indicator
var result: Int = 1;
// Get last bit
var back: Int = n and 1;
// left shift by one
n = n shr 1;
// Execute loop until
while (n > 0 && result == 1)
{
if ((n and 1) == back)
{
// When two consecutive bits are same
result = 0;
}
else
{
back = n and 1;
}
n = n shr 1;
}
// Display given number
print("\n Number : " + num);
if (result == 0)
{
print("\n No");
}
else
{
print("\n Yes");
}
}
}
fun main(args: Array < String > ): Unit
{
var num: Int = 85;
// 85 (1010101)
num = 42;
// 42 (101010)
num = 106;
// 106 (1101010)
num = 2;
// 2 (10)
num = 22;
// 22 (10110)
}``````

#### Output

`````` Number : 85
Yes
Number : 42
Yes
Number : 106
No
Number : 2
Yes
Number : 22
No``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.