Users Online

· Guests Online: 140

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

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

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.

 

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

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 1.26 seconds
10,808,209 unique visits