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)
                y = y / 2;
        if (count == 1)
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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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               

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

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.