Q:

C++ program to Find Nth element of the X-OR sequence

belongs to collection: C++ programs on various topics

0

C++ program to Find Nth element of the X-OR sequence

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C++ program:

#include <iostream>
using namespace std;

int sequence(int n)
{
	int a;
	switch(n%4)  
	{
		case 0:
			a=n;
			break;    
		case 1: 
			a=1; 
			break;       
		case 2:
			a=n+1;
			break;
		case 3: 
			a=0;
			break;
	}
	return a;
}

//main function
int main(){
	int n;
	//input number
	cin>>n;
	
	int result=sequence(n);
	cout<<result<<endl;
	return 0;
}

Output

First run:
127
0

Second run:
5
1

Explanation:

 
    Number      X-OR 1 to N     modulo 4

    1           0001            1 (1) 
    2           0011            2 (n+1)
    3           0000            3 (0)
    4           0100            0 (n)
    5           0001            1 (1)
    6           0111            2 (n+1)
    7           0000            3 (0)
    8           1000            0 (n)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Implementations of FCFS scheduling algorithm using... >>
<< Vectors in C++ Standard Template Library (STL)...