Enter the Number: 100 false
This is a C# Program to check whether the given integer has an alternate pattern.
This C# Program Checks whether the given Integer has an Alternate Pattern.
Take input from the user and check for alternate pattern as shown in the program below.
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:"); num = int.Parse(Console.ReadLine()); 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"); } Console.Read(); } }
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.
Enter the Number: 100 false