Find the only repetitive number between 1 to n-1

Here given code implementation process.

/*
    C program for
    Find the only repetitive number between 1 to n-1
*/
#include <stdio.h>

void findSingleRepetitive(int num[], int n)
{
	int sum = 0;
	// Sum of all array elements
	for (int i = 0; i < n; ++i)
	{
		sum += num[i];
	}
	// Calculate single repeated element by using sum of all array element 
	// Minus sum of array element from 1 to n.
	int result = sum - ((n *(n - 1)) / 2);
	// Display calculated result
	printf("\n %d", result);
}
int main(int argc, char const *argv[])
{
	// array elements, which is every element in range 
	// of 1 to n and 1 elements are repetitive
	int num[] = {
		3 , 1 , 5 , 4 , 5 , 2
	};
	// Get the size of number of elements
	int n = sizeof(num) / sizeof(num[0]);
	// Test
	findSingleRepetitive(num, n);
	return 0;
}

Output

 5
/*
    Java program for
    Find the only repetitive number between 1 to n-1
*/
public class Repetitive
{
	public void findSingleRepetitive(int[] num, int n)
	{
		int sum = 0;
		// Sum of all array elements
		for (int i = 0; i < n; ++i)
		{
			sum += num[i];
		}
		// Calculate single repeated element by using sum of all array element 
		// Minus sum of array element from 1 to n.
		int result = sum - ((n * (n - 1)) / 2);
		// Display calculated result
		System.out.print("\n " + result);
	}
	public static void main(String[] args)
	{
		Repetitive task = new Repetitive();
		// array elements, which is every element in range 
		// of 1 to n and 1 elements are repetitive
		int[] num = {
			3 , 1 , 5 , 4 , 5 , 2
		};
		// Get the size of number of elements
		int n = num.length;
		// Test
		task.findSingleRepetitive(num, n);
	}
}

Output

 5
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program for
    Find the only repetitive number between 1 to n-1
*/
class Repetitive
{
	public: void findSingleRepetitive(int num[], int n)
	{
		int sum = 0;
		// Sum of all array elements
		for (int i = 0; i < n; ++i)
		{
			sum += num[i];
		}
		// Calculate single repeated element by using sum of all array element 
		// Minus sum of array element from 1 to n.
		int result = sum - ((n *(n - 1)) / 2);
		// Display calculated result
		cout << "\n " << result;
	}
};
int main()
{
	Repetitive *task = new Repetitive();
	// array elements, which is every element in range 
	// of 1 to n and 1 elements are repetitive
	int num[] = {
		3 , 1 , 5 , 4 , 5 , 2
	};
	// Get the size of number of elements
	int n = sizeof(num) / sizeof(num[0]);
	// Test
	task->findSingleRepetitive(num, n);
	return 0;
}

Output

 5
// Include namespace system
using System;
/*
    Csharp program for
    Find the only repetitive number between 1 to n-1
*/
public class Repetitive
{
	public void findSingleRepetitive(int[] num, int n)
	{
		int sum = 0;
		// Sum of all array elements
		for (int i = 0; i < n; ++i)
		{
			sum += num[i];
		}
		// Calculate single repeated element by using sum of all array element 
		// Minus sum of array element from 1 to n.
		int result = sum - ((n * (n - 1)) / 2);
		// Display calculated result
		Console.Write("\n " + result);
	}
	public static void Main(String[] args)
	{
		Repetitive task = new Repetitive();
		// array elements, which is every element in range 
		// of 1 to n and 1 elements are repetitive
		int[] num = {
			3 , 1 , 5 , 4 , 5 , 2
		};
		// Get the size of number of elements
		int n = num.Length;
		// Test
		task.findSingleRepetitive(num, n);
	}
}

Output

 5
package main
import "fmt"
/*
    Go program for
    Find the only repetitive number between 1 to n-1
*/

func findSingleRepetitive(num[] int, n int) {
	var sum int = 0
	// Sum of all array elements
	for i := 0 ; i < n ; i++ {
		sum += num[i]
	}
	// Calculate single repeated element by using sum of all array element 
	// Minus sum of array element from 1 to n.
	var result int = sum - ((n * (n - 1)) / 2)
	// Display calculated result
	fmt.Print("\n ", result)
}
func main() {

	// array elements, which is every element in range 
	// of 1 to n and 1 elements are repetitive
	var num = [] int {3 , 1 , 5 , 4 , 5 , 2}
	// Get the size of number of elements
	var n int = len(num)
	// Test
	findSingleRepetitive(num, n)
}

Output

 5
<?php
/*
    Php program for
    Find the only repetitive number between 1 to n-1
*/
class Repetitive
{
	public	function findSingleRepetitive($num, $n)
	{
		$sum = 0;
		// Sum of all array elements
		for ($i = 0; $i < $n; ++$i)
		{
			$sum += $num[$i];
		}
		// Calculate single repeated element by using sum of all array element 
		// Minus sum of array element from 1 to n.
		$result = $sum - ((int)(($n * ($n - 1)) / 2));
		// Display calculated result
		echo("\n ".$result);
	}
}

function main()
{
	$task = new Repetitive();
	// array elements, which is every element in range 
	// of 1 to n and 1 elements are repetitive
	$num = array(3, 1, 5, 4, 5, 2);
	// Get the size of number of elements
	$n = count($num);
	// Test
	$task->findSingleRepetitive($num, $n);
}
main();

Output

 5
/*
    Node JS program for
    Find the only repetitive number between 1 to n-1
*/
class Repetitive
{
	findSingleRepetitive(num, n)
	{
		var sum = 0;
		// Sum of all array elements
		for (var i = 0; i < n; ++i)
		{
			sum += num[i];
		}
		// Calculate single repeated element by using sum of all array element 
		// Minus sum of array element from 1 to n.
		var result = sum - (parseInt((n * (n - 1)) / 2));
		// Display calculated result
		process.stdout.write("\n " + result);
	}
}

function main()
{
	var task = new Repetitive();
	// array elements, which is every element in range 
	// of 1 to n and 1 elements are repetitive
	var num = [3, 1, 5, 4, 5, 2];
	// Get the size of number of elements
	var n = num.length;
	// Test
	task.findSingleRepetitive(num, n);
}
main();

Output

 5
#    Python 3 program for
#    Find the only repetitive number between 1 to n-1
class Repetitive :
	def findSingleRepetitive(self, num, n) :
		sum = 0
		i = 0
		#  Sum of all list elements
		while (i < n) :
			sum += num[i]
			i += 1
		
		#  Calculate single repeated element by using sum of all list element 
		#  Minus sum of list element from 1 to n.
		result = sum - (int((n * (n - 1)) / 2))
		#  Display calculated result
		print("\n ", result, end = "")
	

def main() :
	task = Repetitive()
	#  list elements, which is every element in range 
	#  of 1 to n and 1 elements are repetitive
	num = [3, 1, 5, 4, 5, 2]
	#  Get the size of number of elements
	n = len(num)
	#  Test
	task.findSingleRepetitive(num, n)

if __name__ == "__main__": main()

Output

  5
#    Ruby program for
#    Find the only repetitive number between 1 to n-1
class Repetitive 
	def findSingleRepetitive(num, n) 
		sum = 0
		i = 0
		#  Sum of all array elements
		while (i < n) 
			sum += num[i]
			i += 1
		end

		#  Calculate single repeated element by using sum of all array element 
		#  Minus sum of array element from 1 to n.
		result = sum - ((n * (n - 1)) / 2)
		#  Display calculated result
		print("\n ", result)
	end

end

def main() 
	task = Repetitive.new()
	#  array elements, which is every element in range 
	#  of 1 to n and 1 elements are repetitive
	num = [3, 1, 5, 4, 5, 2]
	#  Get the size of number of elements
	n = num.length
	#  Test
	task.findSingleRepetitive(num, n)
end

main()

Output

 5
/*
    Scala program for
    Find the only repetitive number between 1 to n-1
*/
class Repetitive()
{
	def findSingleRepetitive(num: Array[Int], n: Int): Unit = {
		var sum: Int = 0;
		var i: Int = 0;
		// Sum of all array elements
		while (i < n)
		{
			sum += num(i);
			i += 1;
		}
		// Calculate single repeated element by using sum of all array element 
		// Minus sum of array element from 1 to n.
		var result: Int = sum - ((n * (n - 1)) / 2);
		// Display calculated result
		print("\n " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Repetitive = new Repetitive();
		// array elements, which is every element in range 
		// of 1 to n and 1 elements are repetitive
		var num: Array[Int] = Array(3, 1, 5, 4, 5, 2);
		// Get the size of number of elements
		var n: Int = num.length;
		// Test
		task.findSingleRepetitive(num, n);
	}
}

Output

 5
import Foundation;
/*
    Swift 4 program for
    Find the only repetitive number between 1 to n-1
*/
class Repetitive
{
	func findSingleRepetitive(_ num: [Int], _ n: Int)
	{
		var sum: Int = 0;
		var i: Int = 0;
		// Sum of all array elements
		while (i < n)
		{
			sum += num[i];
			i += 1;
		}
		// Calculate single repeated element by using sum of all array element 
		// Minus sum of array element from 1 to n.
		let result: Int = sum - ((n * (n - 1)) / 2);
		// Display calculated result
		print("\n ", result, terminator: "");
	}
}
func main()
{
	let task: Repetitive = Repetitive();
	// array elements, which is every element in range 
	// of 1 to n and 1 elements are repetitive
	let num: [Int] = [3, 1, 5, 4, 5, 2];
	// Get the size of number of elements
	let n: Int = num.count;
	// Test
	task.findSingleRepetitive(num, n);
}
main();

Output

  5
/*
    Kotlin program for
    Find the only repetitive number between 1 to n-1
*/
class Repetitive
{
	fun findSingleRepetitive(num: Array < Int > , n: Int): Unit
	{
		var sum: Int = 0;
		var i: Int = 0;
		// Sum of all array elements
		while (i < n)
		{
			sum += num[i];
			i += 1;
		}
		// Calculate single repeated element by using sum of all array element 
		// Minus sum of array element from 1 to n.
		val result: Int = sum - ((n * (n - 1)) / 2);
		// Display calculated result
		print("\n " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Repetitive = Repetitive();
	// array elements, which is every element in range 
	// of 1 to n and 1 elements are repetitive
	val num: Array < Int > = arrayOf(3, 1, 5, 4, 5, 2);
	// Get the size of number of elements
	val n: Int = num.count();
	// Test
	task.findSingleRepetitive(num, n);
}

Output

 5


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







© 2021, kalkicode.com, All rights reserved