# Calculate XOR from 1 to n

Here given code implementation process.

``````// C Program
// Calculate XOR from 1 to n
#include <stdio.h>

// Calculate xor from 1 to n numbers
void calculateXor(int num)
{
if (num < 1)
{
return;
}
// First number
int result = 1;
// Second number
int counter = 2;
// Execute loop through by num
while (counter <= num)
{
// Do xor operation
result = result ^ counter;
// next number
counter++;
}
//Display the calculated xor result
printf("\n XOR (1 to %d) is : %d", num, result);
}
int main(int argc, char
const *argv[])
{
// Test Case
calculateXor(4);
calculateXor(10);
calculateXor(13);
calculateXor(16);
calculateXor(19);
return 0;
}``````

#### Output

`````` XOR (1 to 4) is : 4
XOR (1 to 10) is : 11
XOR (1 to 13) is : 1
XOR (1 to 16) is : 16
XOR (1 to 19) is : 0``````
``````/*
Java program
Calculate XOR from 1 to n
*/
public class XorOperation
{
// Calculate xor from 1 to n numbers
public void calculateXor(int num)
{
if (num < 1)
{
return;
}
// First number
int result = 1;
// Second number
int counter = 2;
// Execute loop through by num
while (counter <= num)
{
// Do xor operation
result = result ^ counter;
// next number
counter++;
}
//Display the calculated xor result
System.out.print("\n XOR ( 1 to " + num + " ) is : " + result);
}
public static void main(String[] args)
{
// Test Case
}
}``````

#### Output

`````` XOR ( 1 to 4 ) is : 4
XOR ( 1 to 10 ) is : 11
XOR ( 1 to 13 ) is : 1
XOR ( 1 to 16 ) is : 16
XOR ( 1 to 19 ) is : 0``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ program
Calculate XOR from 1 to n
*/
class XorOperation
{
public:
// Calculate xor from 1 to n numbers
void calculateXor(int num)
{
if (num < 1)
{
return;
}
// First number
int result = 1;
// Second number
int counter = 2;
// Execute loop through by num
while (counter <= num)
{
// Do xor operation
result = result ^ counter;
// next number
counter = counter + 1;
}
//Display the calculated xor result
cout << "\n XOR ( 1 to " << num << " ) is : " << result;
}
};
int main()
{
// Test Case
return 0;
}``````

#### Output

`````` XOR ( 1 to 4 ) is : 4
XOR ( 1 to 10 ) is : 11
XOR ( 1 to 13 ) is : 1
XOR ( 1 to 16 ) is : 16
XOR ( 1 to 19 ) is : 0``````
``````// Include namespace system
using System;
/*
C# program
Calculate XOR from 1 to n
*/
public class XorOperation
{
// Calculate xor from 1 to n numbers
public void calculateXor(int num)
{
if (num < 1)
{
return;
}
// First number
int result = 1;
// Second number
int counter = 2;
// Execute loop through by num
while (counter <= num)
{
// Do xor operation
result = result ^ counter;
// next number
counter = counter + 1;
}
//Display the calculated xor result
Console.Write("\n XOR ( 1 to " + num + " ) is : " + result);
}
public static void Main(String[] args)
{
// Test Case
}
}``````

#### Output

`````` XOR ( 1 to 4 ) is : 4
XOR ( 1 to 10 ) is : 11
XOR ( 1 to 13 ) is : 1
XOR ( 1 to 16 ) is : 16
XOR ( 1 to 19 ) is : 0``````
``````<?php
/*
Php program
Calculate XOR from 1 to n
*/
class XorOperation
{
// Calculate xor from 1 to n numbers
public	function calculateXor(\$num)
{
if (\$num < 1)
{
return;
}
// First number
\$result = 1;
// Second number
\$counter = 2;
// Execute loop through by num
while (\$counter <= \$num)
{
// Do xor operation
\$result = \$result ^ \$counter;
// next number
\$counter = \$counter + 1;
}
//Display the calculated xor result
echo "\n XOR ( 1 to ". \$num ." ) is : ". \$result;
}
}

function main()
{
// Test Case
}
main();``````

#### Output

`````` XOR ( 1 to 4 ) is : 4
XOR ( 1 to 10 ) is : 11
XOR ( 1 to 13 ) is : 1
XOR ( 1 to 16 ) is : 16
XOR ( 1 to 19 ) is : 0``````
``````/*
Node Js program
Calculate XOR from 1 to n
*/
class XorOperation
{
// Calculate xor from 1 to n numbers
calculateXor(num)
{
if (num < 1)
{
return;
}
// First number
var result = 1;
// Second number
var counter = 2;
// Execute loop through by num
while (counter <= num)
{
// Do xor operation
result = result ^ counter;
// next number
counter = counter + 1;
}
//Display the calculated xor result
process.stdout.write("\n XOR ( 1 to " + num + " ) is : " + result);
}
}

function main()
{
// Test Case
}
main();``````

#### Output

`````` XOR ( 1 to 4 ) is : 4
XOR ( 1 to 10 ) is : 11
XOR ( 1 to 13 ) is : 1
XOR ( 1 to 16 ) is : 16
XOR ( 1 to 19 ) is : 0``````
``````#   Python 3 program
#   Calculate XOR from 1 to n

class XorOperation :
#  Calculate xor from 1 to n numbers
def calculateXor(self, num) :
if (num < 1) :
return

#  First number
result = 1
#  Second number
counter = 2
#  Execute loop through by num
while (counter <= num) :
#  Do xor operation
result = result ^ counter
#  next number
counter = counter + 1

# Display the calculated xor result
print("\n XOR ( 1 to ", num ," ) is : ", result, end = "")

def main() :
#  Test Case

if __name__ == "__main__": main()``````

#### Output

`````` XOR ( 1 to  4  ) is :  4
XOR ( 1 to  10  ) is :  11
XOR ( 1 to  13  ) is :  1
XOR ( 1 to  16  ) is :  16
XOR ( 1 to  19  ) is :  0``````
``````#   Ruby program
#   Calculate XOR from 1 to n

class XorOperation
#  Calculate xor from 1 to n numbers
def calculateXor(num)
if (num < 1)
return
end

#  First number
result = 1
#  Second number
counter = 2
#  Execute loop through by num
while (counter <= num)
#  Do xor operation
result = result ^ counter
#  next number
counter = counter + 1
end

# Display the calculated xor result
print("\n XOR ( 1 to ", num ," ) is : ", result)
end

end

def main()
#  Test Case
end

main()``````

#### Output

`````` XOR ( 1 to 4 ) is : 4
XOR ( 1 to 10 ) is : 11
XOR ( 1 to 13 ) is : 1
XOR ( 1 to 16 ) is : 16
XOR ( 1 to 19 ) is : 0``````
``````/*
Scala program
Calculate XOR from 1 to n
*/
class XorOperation
{
// Calculate xor from 1 to n numbers
def calculateXor(num: Int): Unit = {
if (num < 1)
{
return;
}
// First number
var result: Int = 1;
// Second number
var counter: Int = 2;
// Execute loop through by num
while (counter <= num)
{
// Do xor operation
result = result ^ counter;
// next number
counter = counter + 1;
}
//Display the calculated xor result
print("\n XOR ( 1 to " + num + " ) is : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: XorOperation = new XorOperation();
// Test Case
}
}``````

#### Output

`````` XOR ( 1 to 4 ) is : 4
XOR ( 1 to 10 ) is : 11
XOR ( 1 to 13 ) is : 1
XOR ( 1 to 16 ) is : 16
XOR ( 1 to 19 ) is : 0``````
``````/*
Swift 4 program
Calculate XOR from 1 to n
*/
class XorOperation
{
// Calculate xor from 1 to n numbers
func calculateXor(_ num: Int)
{
if (num < 1)
{
return;
}
// First number
var result: Int = 1;
// Second number
var counter: Int = 2;
// Execute loop through by num
while (counter <= num)
{
// Do xor operation
result = result ^ counter;
// next number
counter = counter + 1;
}
//Display the calculated xor result
print("\n XOR ( 1 to ", num ," ) is : ", result, terminator: "");
}
}
func main()
{
// Test Case
}
main();``````

#### Output

`````` XOR ( 1 to  4  ) is :  4
XOR ( 1 to  10  ) is :  11
XOR ( 1 to  13  ) is :  1
XOR ( 1 to  16  ) is :  16
XOR ( 1 to  19  ) is :  0``````
``````/*
Kotlin program
Calculate XOR from 1 to n
*/
class XorOperation
{
// Calculate xor from 1 to n numbers
fun calculateXor(num: Int): Unit
{
if (num < 1)
{
return;
}
// First number
var result: Int = 1;
// Second number
var counter: Int = 2;
// Execute loop through by num
while (counter <= num)
{
// Do xor operation
result = result xor counter;
// next number
counter = counter + 1;
}
//Display the calculated xor result
print("\n XOR ( 1 to " + num + " ) is : " + result);
}
}
fun main(args: Array < String > ): Unit
{
// Test Case
}``````

#### Output

`````` XOR ( 1 to 4 ) is : 4
XOR ( 1 to 10 ) is : 11
XOR ( 1 to 13 ) is : 1
XOR ( 1 to 16 ) is : 16
XOR ( 1 to 19 ) is : 0``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

### New Comment 