# C# Program to Check whether the Given Integer has an Alternate Pattern

This is a C# Program to check whether the given integer has an alternate pattern.

Problem Description

This C# Program Checks whether the given Integer has an Alternate Pattern.

Problem Solution

Take input from the user and check for alternate pattern as shown in the program below.

Program/Source Code

Here is source code of the C# Program to Check whether the given Integer has an Alternate Pattern. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
* C# Program to Check whether the given Integer has an
* Alternate Pattern
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class program
{
public static void Main()
{
int num, x, y, count = 0;
Console.WriteLine("Enter the Number:");
x = num << 1;
y = x ^ num;
y = y + 1;
while ((y / 2) != 0)
{
if (y % 2 != 0)
{
count++;
break;
}
else
{
y = y / 2;
}
}
if (count == 1)
{
Console.WriteLine("false");
}
else
{
Console.WriteLine("true");
}
}
}
Program Explanation

In this C# Program, we are reading the number using ‘num’ variable. Compute the Binary Left Shift Operation, the left operand’s value is moved left by the number of bits specified by the right operand.

The ‘y’ variable is used to compute the Binary XOR operation, copies the bit if it is set in one operand but not both and increment the value of ‘y’ variable by 1. While loop is used to check the number is in powers of 2.

If else condition statement is used to check that modulus of the value of ‘y’ variable by 2 is not equal to 0. If the condition is true, then it will execute the statement and increment the value of ‘count’ variable. Otherwise, if the condition is false, then execute the else statement. Using if else condition statement print the alternate pattern of the given integer.

Runtime Test Cases

Enter the Number: 100
false

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]