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:");
        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();
    }
}
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.

advertisement

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.

Free 30-Day C Certification Bootcamp is Live. Join Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.