Print first n numbers with exactly two set bits

Here given code implementation process.

// C Program
// Print first n numbers with exactly two set bits
#include <stdio.h>

// Print the initial n numbers with only two active bits
void twoSetBits(int n)
{
	if (n <= 0)
	{
		return;
	}
	printf("\n  N : %d \n", n);
	int bit1 = 1;
	int bit2 = 0;
	int counter = 0;
	// Execute loop until count value is less than n
	while (counter < n)
	{
		while (bit2 < bit1 && counter < n)
		{
			// Display the  number which have two active bits
			printf("  %d", ((1 << bit1) | (1 << bit2)));
			bit2++;
			// Increase resultant count
			counter++;
		}
		bit1++;
		// Reset bit two
		bit2 = 0;
	}
	printf("\n");
}
int main()
{
	/*
	    First few numbers which have two active bits 
	    ---------------    
	    3   : (11)
	    5   : (101)
	    6   : (110)
	    9   : (1001)
	    10  : (1010)
	    12  : (1100)
	    17  : (10001)
	    18  : (10010)
	    20  : (10100)
	    24  : (11000)
	    33  : (100001)
	    34  : (100010)
	    36  : (100100)
	    40  : (101000)
	    48  : (110000)
	    65  : (1000001)
	    66  : (1000010)
	    68  : (1000100)
	    72  : (1001000)
	    80  : (1010000)
	*/
	// Test
	twoSetBits(10);
	return 0;
}

Output

  N : 10
  3  5  6  9  10  12  17  18  20  24
/*
  Java Program
  Print first n numbers with exactly two set bits
*/
public class Activity
{
    // Print the initial n numbers with only two active bits
    public void twoSetBits(int n)
    {
        if (n <= 0)
        {
            return;
        }
        System.out.print("\n N : " + n + " \n");
        int bit1 = 1;
        int bit2 = 0;
        int counter = 0;
        while (counter < n)
        {
            while (bit2 < bit1 && counter < n)
            {
                // Display the  number which have two active bits
                System.out.print(" " + ((1 << bit1) | (1 << bit2)) );
                bit2++;
                counter++;
            }
            bit1++;
            // Reset bit two
            bit2 = 0;
        }
        System.out.print("\n");
    }
    public static void main(String[] args)
    {
        Activity task = new Activity();

        /*
            First few numbers which have two active bits 
            ---------------    
            3   : (11)
            5   : (101)
            6   : (110)
            9   : (1001)
            10  : (1010)
            12  : (1100)
            17  : (10001)
            18  : (10010)
            20  : (10100)
            24  : (11000)
            33  : (100001)
            34  : (100010)
            36  : (100100)
            40  : (101000)
            48  : (110000)
            65  : (1000001)
            66  : (1000010)
            68  : (1000100)
            72  : (1001000)
            80  : (1010000)
        */
        task.twoSetBits(10);
    }
}

Output

 N : 10
 3 5 6 9 10 12 17 18 20 24
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program
  Print first n numbers with exactly two set bits
*/
class Activity
{
	public:
		// Print the initial n numbers with only two active bits
		void twoSetBits(int n)
		{
			if (n <= 0)
			{
				return;
			}
			cout << "\n N : " << n << " \n";
			int bit1 = 1;
			int bit2 = 0;
			int counter = 0;
			while (counter < n)
			{
				while (bit2 < bit1 && counter < n)
				{
					// Display the  number which have two active bits
					cout << " " << ((1 << bit1) | (1 << bit2));
					bit2++;
					counter++;
				}
				bit1++;
				// Reset bit two
				bit2 = 0;
			}
			cout << "\n";
		}
};
int main()
{
	Activity task = Activity();
	/*
	    First few numbers which have two active bits 
	    ---------------    
	    3   : (11)
	    5   : (101)
	    6   : (110)
	    9   : (1001)
	    10  : (1010)
	    12  : (1100)
	    17  : (10001)
	    18  : (10010)
	    20  : (10100)
	    24  : (11000)
	    33  : (100001)
	    34  : (100010)
	    36  : (100100)
	    40  : (101000)
	    48  : (110000)
	    65  : (1000001)
	    66  : (1000010)
	    68  : (1000100)
	    72  : (1001000)
	    80  : (1010000) 
	*/
	task.twoSetBits(10);
	return 0;
}

Output

 N : 10
 3 5 6 9 10 12 17 18 20 24
// Include namespace system
using System;
/*
  C# Program
  Print first n numbers with exactly two set bits
*/
public class Activity
{
	// Print the initial n numbers with only two active bits
	public void twoSetBits(int n)
	{
		if (n <= 0)
		{
			return;
		}
		Console.Write("\n N : " + n + " \n");
		int bit1 = 1;
		int bit2 = 0;
		int counter = 0;
		while (counter < n)
		{
			while (bit2 < bit1 && counter < n)
			{
				// Display the  number which have two active bits
				Console.Write(" " + ((1 << bit1) | (1 << bit2)));
				bit2++;
				counter++;
			}
			bit1++;
			// Reset bit two
			bit2 = 0;
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		Activity task = new Activity();
		/*
		    First few numbers which have two active bits 
		    ---------------    
		    3   : (11)
		    5   : (101)
		    6   : (110)
		    9   : (1001)
		    10  : (1010)
		    12  : (1100)
		    17  : (10001)
		    18  : (10010)
		    20  : (10100)
		    24  : (11000)
		    33  : (100001)
		    34  : (100010)
		    36  : (100100)
		    40  : (101000)
		    48  : (110000)
		    65  : (1000001)
		    66  : (1000010)
		    68  : (1000100)
		    72  : (1001000)
		    80  : (1010000) 
		*/
		task.twoSetBits(10);
	}
}

Output

 N : 10
 3 5 6 9 10 12 17 18 20 24
<?php
/*
  Php Program
  Print first n numbers with exactly two set bits
*/
class Activity
{
	// Print the initial n numbers with only two active bits
	public	function twoSetBits($n)
	{
		if ($n <= 0)
		{
			return;
		}
		echo "\n N : ". $n ." \n";
		$bit1 = 1;
		$bit2 = 0;
		$counter = 0;
		while ($counter < $n)
		{
			while ($bit2 < $bit1 && $counter < $n)
			{
				// Display the  number which have two active bits
				echo " ". ((1 << $bit1) | (1 << $bit2));
				$bit2++;
				$counter++;
			}
			$bit1++;
			// Reset bit two
			$bit2 = 0;
		}
		echo "\n";
	}
}

function main()
{
	$task = new Activity();
	/*
	    First few numbers which have two active bits 
	    ---------------    
	    3   : (11)
	    5   : (101)
	    6   : (110)
	    9   : (1001)
	    10  : (1010)
	    12  : (1100)
	    17  : (10001)
	    18  : (10010)
	    20  : (10100)
	    24  : (11000)
	    33  : (100001)
	    34  : (100010)
	    36  : (100100)
	    40  : (101000)
	    48  : (110000)
	    65  : (1000001)
	    66  : (1000010)
	    68  : (1000100)
	    72  : (1001000)
	    80  : (1010000) 
	*/
	$task->twoSetBits(10);
}
main();

Output

 N : 10
 3 5 6 9 10 12 17 18 20 24
/*
  Node Js Program
  Print first n numbers with exactly two set bits
*/
class Activity
{
	// Print the initial n numbers with only two active bits
	twoSetBits(n)
	{
		if (n <= 0)
		{
			return;
		}
		process.stdout.write("\n N : " + n + " \n");
		var bit1 = 1;
		var bit2 = 0;
		var counter = 0;
		while (counter < n)
		{
			while (bit2 < bit1 && counter < n)
			{
				// Display the  number which have two active bits
				process.stdout.write(" " + ((1 << bit1) | (1 << bit2)));
				bit2++;
				counter++;
			}
			bit1++;
			// Reset bit two
			bit2 = 0;
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var task = new Activity();
	/*
	    First few numbers which have two active bits 
	    ---------------    
	    3   : (11)
	    5   : (101)
	    6   : (110)
	    9   : (1001)
	    10  : (1010)
	    12  : (1100)
	    17  : (10001)
	    18  : (10010)
	    20  : (10100)
	    24  : (11000)
	    33  : (100001)
	    34  : (100010)
	    36  : (100100)
	    40  : (101000)
	    48  : (110000)
	    65  : (1000001)
	    66  : (1000010)
	    68  : (1000100)
	    72  : (1001000)
	    80  : (1010000) 
	*/
	task.twoSetBits(10);
}
main();

Output

 N : 10
 3 5 6 9 10 12 17 18 20 24
#   Python 3 Program
#   Print first n numbers with exactly two set bits

class Activity :
	#  Print the initial n numbers with only two active bits
	def twoSetBits(self, n) :
		if (n <= 0) :
			return
		
		print("\n  N : ", n ," ")
		bit1 = 1
		bit2 = 0
		counter = 0
		while (counter < n) :
			while (bit2 < bit1 and counter < n) :
				#  Display the  number which have two active bits
				print(" ", ((1 << bit1) | (1 << bit2)), end = "")
				bit2 += 1
				counter += 1
			
			bit1 += 1
			#  Reset bit two
			bit2 = 0
		
		print(end = "\n")
	

def main() :
	task = Activity()
	# 
	#     First few numbers which have two active bits 
	#     ---------------    
	#     3   : (11)
	#     5   : (101)
	#     6   : (110)
	#     9   : (1001)
	#     10  : (1010)
	#     12  : (1100)
	#     17  : (10001)
	#     18  : (10010)
	#     20  : (10100)
	#     24  : (11000)
	#     33  : (100001)
	#     34  : (100010)
	#     36  : (100100)
	#     40  : (101000)
	#     48  : (110000)
	#     65  : (1000001)
	#     66  : (1000010)
	#     68  : (1000100)
	#     72  : (1001000)
	#     80  : (1010000) 
	
	task.twoSetBits(10)

if __name__ == "__main__": main()

Output

  N :  10
  3  5  6  9  10  12  17  18  20  24
#   Ruby Program
#   Print first n numbers with exactly two set bits

class Activity 
	#  Print the initial n numbers with only two active bits
	def twoSetBits(n) 
		if (n <= 0) 
			return
		end

		print("\n N : ", n ," \n")
		bit1 = 1
		bit2 = 0
		counter = 0
		while (counter < n) 
			while (bit2 < bit1 && counter < n) 
				#  Display the  number which have two active bits
				print(" ", ((1 << bit1) | (1 << bit2)))
				bit2 += 1
				counter += 1
			end

			bit1 += 1
			#  Reset bit two
			bit2 = 0
		end

		print("\n")
	end

end

def main() 
	task = Activity.new()
	# 
	#     First few numbers which have two active bits 
	#     ---------------    
	#     3   : (11)
	#     5   : (101)
	#     6   : (110)
	#     9   : (1001)
	#     10  : (1010)
	#     12  : (1100)
	#     17  : (10001)
	#     18  : (10010)
	#     20  : (10100)
	#     24  : (11000)
	#     33  : (100001)
	#     34  : (100010)
	#     36  : (100100)
	#     40  : (101000)
	#     48  : (110000)
	#     65  : (1000001)
	#     66  : (1000010)
	#     68  : (1000100)
	#     72  : (1001000)
	#     80  : (1010000) 
	
	task.twoSetBits(10)
end

main()

Output

 N : 10 
 3 5 6 9 10 12 17 18 20 24
/*
  Scala Program
  Print first n numbers with exactly two set bits
*/
class Activity
{
	// Print the initial n numbers with only two active bits
	def twoSetBits(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		print("\n N : " + n + " \n");
		var bit1: Int = 1;
		var bit2: Int = 0;
		var counter: Int = 0;
		while (counter < n)
		{
			while (bit2 < bit1 && counter < n)
			{
				// Display the  number which have two active bits
				print(" " + ((1 << bit1) | (1 << bit2)));
				bit2 += 1;
				counter += 1;
			}
			bit1 += 1;
			// Reset bit two
			bit2 = 0;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Activity = new Activity();
		/*
		    First few numbers which have two active bits 
		    ---------------    
		    3   : (11)
		    5   : (101)
		    6   : (110)
		    9   : (1001)
		    10  : (1010)
		    12  : (1100)
		    17  : (10001)
		    18  : (10010)
		    20  : (10100)
		    24  : (11000)
		    33  : (100001)
		    34  : (100010)
		    36  : (100100)
		    40  : (101000)
		    48  : (110000)
		    65  : (1000001)
		    66  : (1000010)
		    68  : (1000100)
		    72  : (1001000)
		    80  : (1010000) 
		*/
		task.twoSetBits(10);
	}
}

Output

 N : 10
 3 5 6 9 10 12 17 18 20 24
/*
  Swift 4 Program
  Print first n numbers with exactly two set bits
*/
class Activity
{
	// Print the initial n numbers with only two active bits
	func twoSetBits(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		print("\n N : ", n ," ");
		var bit1: Int = 1;
		var bit2: Int = 0;
		var counter: Int = 0;
		while (counter < n)
		{
			while (bit2 < bit1 && counter < n)
			{
				// Display the  number which have two active bits
				print(" ", ((1 << bit1) | (1 << bit2)), terminator: "");
				bit2 += 1;
				counter += 1;
			}
			bit1 += 1;
			// Reset bit two
			bit2 = 0;
		}
		print(terminator: "\n");
	}
}
func main()
{
	let task: Activity = Activity();
	/*
	    First few numbers which have two active bits 
	    ---------------    
	    3   : (11)
	    5   : (101)
	    6   : (110)
	    9   : (1001)
	    10  : (1010)
	    12  : (1100)
	    17  : (10001)
	    18  : (10010)
	    20  : (10100)
	    24  : (11000)
	    33  : (100001)
	    34  : (100010)
	    36  : (100100)
	    40  : (101000)
	    48  : (110000)
	    65  : (1000001)
	    66  : (1000010)
	    68  : (1000100)
	    72  : (1001000)
	    80  : (1010000) 
	*/
	task.twoSetBits(10);
}
main();

Output

 N :  10
  3  5  6  9  10  12  17  18  20  24
/*
  Kotlin Program
  Print first n numbers with exactly two set bits
*/
class Activity
{
	// Print the initial n numbers with only two active bits
	fun twoSetBits(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		print("\n N : " + n + " \n");
		var bit1: Int = 1;
		var bit2: Int = 0;
		var counter: Int = 0;
		while (counter < n)
		{
			while (bit2 < bit1 && counter < n)
			{
				// Display the  number which have two active bits
				print(" " + ((1 shl bit1) or (1 shl bit2)));
				bit2 += 1;
				counter += 1;
			}
			bit1 += 1;
			// Reset bit two
			bit2 = 0;
		}
		print("\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Activity = Activity();
	/*
	    First few numbers which have two active bits 
	    ---------------    
	    3   : (11)
	    5   : (101)
	    6   : (110)
	    9   : (1001)
	    10  : (1010)
	    12  : (1100)
	    17  : (10001)
	    18  : (10010)
	    20  : (10100)
	    24  : (11000)
	    33  : (100001)
	    34  : (100010)
	    36  : (100100)
	    40  : (101000)
	    48  : (110000)
	    65  : (1000001)
	    66  : (1000010)
	    68  : (1000100)
	    72  : (1001000)
	    80  : (1010000) 
	*/
	task.twoSetBits(10);
}

Output

 N : 10
 3 5 6 9 10 12 17 18 20 24

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