Skip to main content

Find the length of longest consecutive 1s in binary representation

Here given code implementation process.

// C Program
// Find the length of longest consecutive 1s in binary representation
#include <stdio.h>

// Find the length of longest consecutive 1s
void consecutiveOnes(int num)
{
	if (num < 0)
	{
		// When the number is negative
		return;
	}
	// Define some auxiliary counters variables
	int counter = 0;
	int result = 0;
	// Get num value
	int n = num;
	// Execute loop until n is not zero
	while (n != 0)
	{
		if (n & 1 == 1)
		{
			counter++;
			if (counter > result)
			{
				result = counter;
			}
		}
		else
		{
			// Reset counter
			counter = 0;
		}
		// Shift one by right
		n = n >> 1;
	}
	// Display given number
	printf("\n Number : %d", num);
	// Display calculated result result
	printf("\n Longest Consecutive Ones : %d", result);
}
int main()
{
	// 123 (01111011)
	consecutiveOnes(123);
	// 16  (10000)
	consecutiveOnes(16);
	// 29  (11101)
	consecutiveOnes(29);
	return 0;
}

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
/*
  Java Program
  Find the length of longest consecutive 1s in binary representation
*/
public class Counting
{
	// Find the length of longest consecutive 1s
	public void consecutiveOnes(int num)
	{
		if (num < 0)
		{
			// When the number is negative
			return;
		}
		// Define some auxiliary counters variables
		int counter = 0;
		int result = 0;
		// Get num value
		int n = num;
		// Execute loop until n is not zero
		while (n != 0)
		{
			if ((n & 1) == 1)
			{
				counter++;
				if (counter > result)
				{
					result = counter;
				}
			}
			else
			{
				// Reset counter
				counter = 0;
			}
			// Shift one by right
			n = n >> 1;
		}
		// Display given number
		System.out.print("\n Number : " + num );
		// Display calculated result result
		System.out.print("\n Longest Consecutive Ones : " + result );
	}
	public static void main(String[] args)
	{
		Counting task = new Counting();
		// 123 (01111011)
		task.consecutiveOnes(123);
		// 16  (10000)
		task.consecutiveOnes(16);
		// 29  (11101)
		task.consecutiveOnes(29);
	}
}

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
// Include header file
#include <iostream>
using namespace std;
/*
  C++ Program
  Find the length of longest consecutive 1s in binary representation
*/
class Counting
{
	public:
		// Find the length of longest consecutive 1s
		void consecutiveOnes(int num)
		{
			// When the number is negative
			if (num < 0)
			{
				return;
			}
			// Define some auxiliary counters variables
			int counter = 0;
			int result = 0;
			// Get num value
			int n = num;
			// Execute loop until n is not zero
			while (n != 0)
			{
				if ((n &1) == 1)
				{
					counter++;
					if (counter > result)
					{
						result = counter;
					}
				}
				else
				{
					// Reset counter
					counter = 0;
				}
				// Shift one by right
				n = n >> 1;
			}
			// Display given number
			cout << "\n Number : " << num;
			// Display calculated result result
			cout << "\n Longest Consecutive Ones : " << result;
		}
};
int main()
{
	Counting task = Counting();
	// 123 (01111011)
	task.consecutiveOnes(123);
	// 16  (10000)
	task.consecutiveOnes(16);
	// 29  (11101)
	task.consecutiveOnes(29);
	return 0;
}

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
// Include namespace system
using System;
/*
  C# Program
  Find the length of longest consecutive 1s in binary representation
*/
public class Counting
{
	// Find the length of longest consecutive 1s
	public void consecutiveOnes(int num)
	{
		// When the number is negative
		if (num < 0)
		{
			return;
		}
		// Define some auxiliary counters variables
		int counter = 0;
		int result = 0;
		// Get num value
		int n = num;
		// Execute loop until n is not zero
		while (n != 0)
		{
			if ((n & 1) == 1)
			{
				counter++;
				if (counter > result)
				{
					result = counter;
				}
			}
			else
			{
				// Reset counter
				counter = 0;
			}
			// Shift one by right
			n = n >> 1;
		}
		// Display given number
		Console.Write("\n Number : " + num);
		// Display calculated result result
		Console.Write("\n Longest Consecutive Ones : " + result);
	}
	public static void Main(String[] args)
	{
		Counting task = new Counting();
		// 123 (01111011)
		task.consecutiveOnes(123);
		// 16  (10000)
		task.consecutiveOnes(16);
		// 29  (11101)
		task.consecutiveOnes(29);
	}
}

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
<?php
/*
  Php Program
  Find the length of longest consecutive 1s in binary representation
*/
class Counting
{
	// Find the length of longest consecutive 1s
	public	function consecutiveOnes($num)
	{
		// When the number is negative
		if ($num < 0)
		{
			return;
		}
		// Define some auxiliary counters variables
		$counter = 0;
		$result = 0;
		// Get num value
		$n = $num;
		// Execute loop until n is not zero
		while ($n != 0)
		{
			if (($n & 1) == 1)
			{
				$counter++;
				if ($counter > $result)
				{
					$result = $counter;
				}
			}
			else
			{
				// Reset counter
				$counter = 0;
			}
			// Shift one by right
			$n = $n >> 1;
		}
		// Display given number
		echo "\n Number : ". $num;
		// Display calculated result result
		echo "\n Longest Consecutive Ones : ". $result;
	}
}

function main()
{
	$task = new Counting();
	// 123 (01111011)
	$task->consecutiveOnes(123);
	// 16  (10000)
	$task->consecutiveOnes(16);
	// 29  (11101)
	$task->consecutiveOnes(29);
}
main();

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
/*
  Node Js Program
  Find the length of longest consecutive 1s in binary representation
*/
class Counting
{
	// Find the length of longest consecutive 1s
	consecutiveOnes(num)
	{
		// When the number is negative
		if (num < 0)
		{
			return;
		}
		// Define some auxiliary counters variables
		var counter = 0;
		var result = 0;
		// Get num value
		var n = num;
		// Execute loop until n is not zero
		while (n != 0)
		{
			if ((n & 1) == 1)
			{
				counter++;
				if (counter > result)
				{
					result = counter;
				}
			}
			else
			{
				// Reset counter
				counter = 0;
			}
			// Shift one by right
			n = n >> 1;
		}
		// Display given number
		process.stdout.write("\n Number : " + num);
		// Display calculated result result
		process.stdout.write("\n Longest Consecutive Ones : " + result);
	}
}

function main()
{
	var task = new Counting();
	// 123 (01111011)
	task.consecutiveOnes(123);
	// 16  (10000)
	task.consecutiveOnes(16);
	// 29  (11101)
	task.consecutiveOnes(29);
}
main();

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
#   Python 3 Program
#   Find the length of longest consecutive 1s in binary representation

class Counting :
	#  Find the length of longest consecutive 1s
	def consecutiveOnes(self, num) :
		if (num < 0) :
			#  When the number is negative
			return
		
		#  Define some auxiliary counters variables
		counter = 0
		result = 0
		#  Get num value
		n = num
		#  Execute loop until n is not zero
		while (n != 0) :
			if ((n & 1) == 1) :
				counter += 1
				if (counter > result) :
					result = counter
				
			else :
				#  Reset counter
				counter = 0
			
			#  Shift one by right
			n = n >> 1
		
		#  Display given number
		print("\n Number : ", num, end = "")
		#  Display calculated result result
		print("\n Longest Consecutive Ones : ", result, end = "")
	

def main() :
	task = Counting()
	#  123 (01111011)
	task.consecutiveOnes(123)
	#  16  (10000)
	task.consecutiveOnes(16)
	#  29  (11101)
	task.consecutiveOnes(29)

if __name__ == "__main__": main()

Output

 Number :  123
 Longest Consecutive Ones :  4
 Number :  16
 Longest Consecutive Ones :  1
 Number :  29
 Longest Consecutive Ones :  3
#   Ruby Program
#   Find the length of longest consecutive 1s in binary representation

class Counting 
	#  Find the length of longest consecutive 1s
	def consecutiveOnes(num) 
		if (num < 0) 
			#  When the number is negative
			return
		end

		#  Define some auxiliary counters variables
		counter = 0
		result = 0
		#  Get num value
		n = num
		#  Execute loop until n is not zero
		while (n != 0) 
			if ((n & 1) == 1) 
				counter += 1
				if (counter > result) 
					result = counter
				end

			else 
				#  Reset counter
				counter = 0
			end

			#  Shift one by right
			n = n >> 1
		end

		#  Display given number
		print("\n Number : ", num)
		#  Display calculated result result
		print("\n Longest Consecutive Ones : ", result)
	end

end

def main() 
	task = Counting.new()
	#  123 (01111011)
	task.consecutiveOnes(123)
	#  16  (10000)
	task.consecutiveOnes(16)
	#  29  (11101)
	task.consecutiveOnes(29)
end

main()

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
/*
  Scala Program
  Find the length of longest consecutive 1s in binary representation
*/
class Counting
{
	// Find the length of longest consecutive 1s
	def consecutiveOnes(num: Int): Unit = {
		// When the number is negative
		if (num < 0)
		{
			return;
		}
		// Define some auxiliary counters variables
		var counter: Int = 0;
		var result: Int = 0;
		// Get num value
		var n: Int = num;
		// Execute loop until n is not zero
		while (n != 0)
		{
			if ((n & 1) == 1)
			{
				counter += 1;
				if (counter > result)
				{
					result = counter;
				}
			}
			else
			{
				// Reset counter
				counter = 0;
			}
			// Shift one by right
			n = n >> 1;
		}
		// Display given number
		print("\n Number : " + num);
		// Display calculated result result
		print("\n Longest Consecutive Ones : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Counting = new Counting();
		// 123 (01111011)
		task.consecutiveOnes(123);
		// 16  (10000)
		task.consecutiveOnes(16);
		// 29  (11101)
		task.consecutiveOnes(29);
	}
}

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
/*
  Swift 4 Program
  Find the length of longest consecutive 1s in binary representation
*/
class Counting
{
	// Find the length of longest consecutive 1s
	func consecutiveOnes(_ num: Int)
	{
		// When the number is negative
		if (num < 0)
		{
			return;
		}
		// Define some auxiliary counters variables
		var counter: Int = 0;
		var result: Int = 0;
		// Get num value
		var n: Int = num;
		// Execute loop until n is not zero
		while (n  != 0)
		{
			if ((n & 1) == 1)
			{
				counter += 1;
				if (counter > result)
				{
					result = counter;
				}
			}
			else
			{
				// Reset counter
				counter = 0;
			}
			// Shift one by right
			n = n >> 1;
		}
		// Display given number
		print("\n Number : ", num, terminator: "");
		// Display calculated result result
		print("\n Longest Consecutive Ones : ", result, terminator: "");
	}
}
func main()
{
	let task: Counting = Counting();
	// 123 (01111011)
	task.consecutiveOnes(123);
	// 16  (10000)
	task.consecutiveOnes(16);
	// 29  (11101)
	task.consecutiveOnes(29);
}
main();

Output

 Number :  123
 Longest Consecutive Ones :  4
 Number :  16
 Longest Consecutive Ones :  1
 Number :  29
 Longest Consecutive Ones :  3
/*
  Kotlin Program
  Find the length of longest consecutive 1s in binary representation
*/
class Counting
{
	// Find the length of longest consecutive 1s
	fun consecutiveOnes(num: Int): Unit
	{
		// When the number is negative
		if (num < 0)
		{
			return;
		}
		// Define some auxiliary counters variables
		var counter: Int = 0;
		var result: Int = 0;
		// Get num value
		var n: Int = num;
		// Execute loop until n is not zero
		while (n != 0)
		{
			if ((n and 1) == 1)
			{
				counter += 1;
				if (counter > result)
				{
					result = counter;
				}
			}
			else
			{
				// Reset counter
				counter = 0;
			}
			// Shift one by right
			n = n shr 1;
		}
		// Display given number
		print("\n Number : " + num);
		// Display calculated result result
		print("\n Longest Consecutive Ones : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Counting = Counting();
	// 123 (01111011)
	task.consecutiveOnes(123);
	// 16  (10000)
	task.consecutiveOnes(16);
	// 29  (11101)
	task.consecutiveOnes(29);
}

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3

In above solution is takes O(n) time to find longest consecutive ones. where n length of binary digits. We can optimize this solution.

// C Program
// Find the length of longest consecutive 1s in binary representation Set B
#include <stdio.h>

// Find the length of longest consecutive 1s
void consecutiveOnes(int num)
{
    
    if(num < 0)
    {
        // When the number is negative
        return;
    }
    // Define some auxiliary counters variable
    int result = 0;
    // Get num value
    int n = num;
    // Execute loop until n is not zero
    while(n != 0)
    {
        n = (n >> 1) & n;
        result++;
    }
    // Display given number
    printf("\n Number : %d",num);
    // Display calculated result result
    printf("\n Longest Consecutive Ones : %d",result);

}

int main()
{
    // 123 (01111011)
    consecutiveOnes(123);
    // 16  (10000)
    consecutiveOnes(16);
    // 29  (11101)
    consecutiveOnes(29);
    return 0;
}

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
/*
  Java Program
  Find the length of longest consecutive 1s in binary representation Set B
*/
public class Counting
{
	// Find the length of longest consecutive 1s
	public void consecutiveOnes(int num)
	{
		if (num < 0)
		{
			// When the number is negative
			return;
		}
		// Define some auxiliary counters variable
		int result = 0;
		// Get num value
		int n = num;
		// Execute loop until n is not zero
		while (n != 0)
		{
			n = (n >> 1) & n;
			result++;
		}
		// Display given number
		System.out.print("\n Number : " + num);
		// Display calculated result result
		System.out.print("\n Longest Consecutive Ones : " + result);
	}
	public static void main(String[] args)
	{
		Counting task = new Counting();
		// 123 (01111011)
		task.consecutiveOnes(123);
		// 16  (10000)
		task.consecutiveOnes(16);
		// 29  (11101)
		task.consecutiveOnes(29);
	}
}

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program
  Find the length of longest consecutive 1s in binary representation Set B
*/

class Counting
{
	public:
		// Find the length of longest consecutive 1s
		void consecutiveOnes(int num)
		{
			// When the number is negative
			if (num < 0)
			{
				return;
			}
			// Define some auxiliary counters variable
			int result = 0;
			// Get num value
			int n = num;
			// Execute loop until n is not zero
			while (n != 0)
			{
				n = (n >> 1) &n;
				result++;
			}
			// Display given number
			cout << "\n Number : " << num;
			// Display calculated result result
			cout << "\n Longest Consecutive Ones : " << result;
		}
};
int main()
{
	Counting task = Counting();
	// 123 (01111011)
	task.consecutiveOnes(123);
	// 16  (10000)
	task.consecutiveOnes(16);
	// 29  (11101)
	task.consecutiveOnes(29);
	return 0;
}

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
// Include namespace system
using System;
/*
  C# Program
  Find the length of longest consecutive 1s in binary representation Set B
*/
public class Counting
{
	// Find the length of longest consecutive 1s
	public void consecutiveOnes(int num)
	{
		// When the number is negative
		if (num < 0)
		{
			return;
		}
		// Define some auxiliary counters variable
		int result = 0;
		// Get num value
		int n = num;
		// Execute loop until n is not zero
		while (n != 0)
		{
			n = (n >> 1) & n;
			result++;
		}
		// Display given number
		Console.Write("\n Number : " + num);
		// Display calculated result result
		Console.Write("\n Longest Consecutive Ones : " + result);
	}
	public static void Main(String[] args)
	{
		Counting task = new Counting();
		// 123 (01111011)
		task.consecutiveOnes(123);
		// 16  (10000)
		task.consecutiveOnes(16);
		// 29  (11101)
		task.consecutiveOnes(29);
	}
}

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
<?php
/*
  Php Program
  Find the length of longest consecutive 1s in binary representation Set B
*/
class Counting
{
	// Find the length of longest consecutive 1s
	public	function consecutiveOnes($num)
	{
		// When the number is negative
		if ($num < 0)
		{
			return;
		}
		// Define some auxiliary counters variable
		$result = 0;
		// Get num value
		$n = $num;
		// Execute loop until n is not zero
		while ($n != 0)
		{
			$n = ($n >> 1) & $n;
			$result++;
		}
		// Display given number
		echo "\n Number : ". $num;
		// Display calculated result result
		echo "\n Longest Consecutive Ones : ". $result;
	}
}

function main()
{
	$task = new Counting();
	// 123 (01111011)
	$task->consecutiveOnes(123);
	// 16  (10000)
	$task->consecutiveOnes(16);
	// 29  (11101)
	$task->consecutiveOnes(29);
}
main();

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
/*
  Node Js Program
  Find the length of longest consecutive 1s in binary representation Set B
*/
class Counting
{
	// Find the length of longest consecutive 1s
	consecutiveOnes(num)
	{
		// When the number is negative
		if (num < 0)
		{
			return;
		}
		// Define some auxiliary counters variable
		var result = 0;
		// Get num value
		var n = num;
		// Execute loop until n is not zero
		while (n != 0)
		{
			n = (n >> 1) & n;
			result++;
		}
		// Display given number
		process.stdout.write("\n Number : " + num);
		// Display calculated result result
		process.stdout.write("\n Longest Consecutive Ones : " + result);
	}
}

function main()
{
	var task = new Counting();
	// 123 (01111011)
	task.consecutiveOnes(123);
	// 16  (10000)
	task.consecutiveOnes(16);
	// 29  (11101)
	task.consecutiveOnes(29);
}
main();

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
#   Python 3 Program
#   Find the length of longest consecutive 1s in binary representation Set B

class Counting :
	#  Find the length of longest consecutive 1s
	def consecutiveOnes(self, num) :
		if (num < 0) :
			#  When the number is negative
			return
		
		#  Define some auxiliary counters variable
		result = 0
		#  Get num value
		n = num
		#  Execute loop until n is not zero
		while (n != 0) :
			n = (n >> 1) & n
			result += 1
		
		#  Display given number
		print("\n Number : ", num, end = "")
		#  Display calculated result result
		print("\n Longest Consecutive Ones : ", result, end = "")
	

def main() :
	task = Counting()
	#  123 (01111011)
	task.consecutiveOnes(123)
	#  16  (10000)
	task.consecutiveOnes(16)
	#  29  (11101)
	task.consecutiveOnes(29)

if __name__ == "__main__": main()

Output

 Number :  123
 Longest Consecutive Ones :  4
 Number :  16
 Longest Consecutive Ones :  1
 Number :  29
 Longest Consecutive Ones :  3
#   Ruby Program
#   Find the length of longest consecutive 1s in binary representation Set B

class Counting 
	#  Find the length of longest consecutive 1s
	def consecutiveOnes(num) 
		if (num < 0) 
			#  When the number is negative
			return
		end

		#  Define some auxiliary counters variable
		result = 0
		#  Get num value
		n = num
		#  Execute loop until n is not zero
		while (n != 0) 
			n = (n >> 1) & n
			result += 1
		end

		#  Display given number
		print("\n Number : ", num)
		#  Display calculated result result
		print("\n Longest Consecutive Ones : ", result)
	end

end

def main() 
	task = Counting.new()
	#  123 (01111011)
	task.consecutiveOnes(123)
	#  16  (10000)
	task.consecutiveOnes(16)
	#  29  (11101)
	task.consecutiveOnes(29)
end

main()

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
/*
  Scala Program
  Find the length of longest consecutive 1s in binary representation Set B
*/
class Counting
{
	// Find the length of longest consecutive 1s
	def consecutiveOnes(num: Int): Unit = {
		// When the number is negative
		if (num < 0)
		{
			return;
		}
		// Define some auxiliary counters variable
		var result: Int = 0;
		// Get num value
		var n: Int = num;
		// Execute loop until n is not zero
		while (n != 0)
		{
			n = (n >> 1) & n;
			result += 1;
		}
		// Display given number
		print("\n Number : " + num);
		// Display calculated result result
		print("\n Longest Consecutive Ones : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Counting = new Counting();
		// 123 (01111011)
		task.consecutiveOnes(123);
		// 16  (10000)
		task.consecutiveOnes(16);
		// 29  (11101)
		task.consecutiveOnes(29);
	}
}

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3
/*
  Swift 4 Program
  Find the length of longest consecutive 1s in binary representation Set B
*/
class Counting
{
	// Find the length of longest consecutive 1s
	func consecutiveOnes(_ num: Int)
	{
		// When the number is negative
		if (num < 0)
		{
			return;
		}
		// Define some auxiliary counters variable
		var result: Int = 0;
		// Get num value
		var n: Int = num;
		// Execute loop until n is not zero
		while (n  != 0)
		{
			n = (n >> 1) & n;
			result += 1;
		}
		// Display given number
		print("\n Number : ", num, terminator: "");
		// Display calculated result result
		print("\n Longest Consecutive Ones : ", result, terminator: "");
	}
}
func main()
{
	let task: Counting = Counting();
	// 123 (01111011)
	task.consecutiveOnes(123);
	// 16  (10000)
	task.consecutiveOnes(16);
	// 29  (11101)
	task.consecutiveOnes(29);
}
main();

Output

 Number :  123
 Longest Consecutive Ones :  4
 Number :  16
 Longest Consecutive Ones :  1
 Number :  29
 Longest Consecutive Ones :  3
/*
  Kotlin Program
  Find the length of longest consecutive 1s in binary representation Set B
*/
class Counting
{
	// Find the length of longest consecutive 1s
	fun consecutiveOnes(num: Int): Unit
	{
		// When the number is negative
		if (num < 0)
		{
			return;
		}
		// Define some auxiliary counters variable
		var result: Int = 0;
		// Get num value
		var n: Int = num;
		// Execute loop until n is not zero
		while (n != 0)
		{
			n = (n shr 1) and n;
			result += 1;
		}
		// Display given number
		print("\n Number : " + num);
		// Display calculated result result
		print("\n Longest Consecutive Ones : " + result);
	}
}
fun main(args: Array <String> ): Unit
{
	var task: Counting = Counting();
	// 123 (01111011)
	task.consecutiveOnes(123);
	// 16  (10000)
	task.consecutiveOnes(16);
	// 29  (11101)
	task.consecutiveOnes(29);
}

Output

 Number : 123
 Longest Consecutive Ones : 4
 Number : 16
 Longest Consecutive Ones : 1
 Number : 29
 Longest Consecutive Ones : 3




Comment

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