Find nth magic number

Here given code implementation process.

// C Program 
// Find nth magic number
#include <stdio.h>

// Find the nth magic number
void findNthMagicNo(int n)
{
	if (n < 0)
	{
		return;
	}
	int power = 5;
	int result = 0;
	int num = n;
	// Execute loop until n is less than 1
	while (num > 0)
	{
		if ((num & 1) == 1)
		{
			// When last bit are active
			result += power;
		}
		// Increase power of 5
		power *= 5;
		// Divide the number by 2
		num = num / 2;
	}
	// Display calculated result
	printf(" %d-th magic number is : %d\n", n, result);
}
int main()
{
	// Test cases
	findNthMagicNo(4);
	findNthMagicNo(7);
	findNthMagicNo(10);
	return 0;
}

Output

 4-th magic number is : 125
 7-th magic number is : 155
 10-th magic number is : 650
/*
  Java Program for
  Find nth magic number
*/
public class MagicNumber
{
	// Find the nth magic number
	public void findNthMagicNo(int n)
	{
		if (n < 0)
		{
			return;
		}
		int power = 5;
		int result = 0;
		int num = n;
		// Execute loop until n is less than 1
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				// When last bit are active
				result += power;
			}
			// Increase power of 5
			power *= 5;
			// Divide the number by 2
			num = num / 2;
		}
		// Display calculated result
		System.out.print(" " + n + "-th magic number is : " + result + "\n");
	}
	public static void main(String[] args)
	{
		MagicNumber task = new MagicNumber();
		// Test cases
		task.findNthMagicNo(4);
		task.findNthMagicNo(7);
		task.findNthMagicNo(10);
	}
}

Output

 4-th magic number is : 125
 7-th magic number is : 155
 10-th magic number is : 650
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Find nth magic number
*/
class MagicNumber
{
	public:
		// Find the nth magic number
		void findNthMagicNo(int n)
		{
			if (n < 0)
			{
				return;
			}
			int power = 5;
			int result = 0;
			int num = n;
			// Execute loop until n is less than 1
			while (num > 0)
			{
				if ((num &1) == 1)
				{
					// When last bit are active
					result += power;
				}
				// Increase power of 5
				power *= 5;
				// Divide the number by 2
				num = num / 2;
			}
			// Display calculated result
			cout << " " << n << "-th magic number is : " << result << "\n";
		}
};
int main()
{
	MagicNumber task = MagicNumber();
	// Test cases
	task.findNthMagicNo(4);
	task.findNthMagicNo(7);
	task.findNthMagicNo(10);
	return 0;
}

Output

 4-th magic number is : 125
 7-th magic number is : 155
 10-th magic number is : 650
// Include namespace system
using System;
/*
  C# Program for
  Find nth magic number
*/
public class MagicNumber
{
	// Find the nth magic number
	public void findNthMagicNo(int n)
	{
		if (n < 0)
		{
			return;
		}
		int power = 5;
		int result = 0;
		int num = n;
		// Execute loop until n is less than 1
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				// When last bit are active
				result += power;
			}
			// Increase power of 5
			power *= 5;
			// Divide the number by 2
			num = num / 2;
		}
		// Display calculated result
		Console.Write(" " + n + "-th magic number is : " + result + "\n");
	}
	public static void Main(String[] args)
	{
		MagicNumber task = new MagicNumber();
		// Test cases
		task.findNthMagicNo(4);
		task.findNthMagicNo(7);
		task.findNthMagicNo(10);
	}
}

Output

 4-th magic number is : 125
 7-th magic number is : 155
 10-th magic number is : 650
<?php
/*
  Php Program for
  Find nth magic number
*/
class MagicNumber
{
	// Find the nth magic number
	public	function findNthMagicNo($n)
	{
		if ($n < 0)
		{
			return;
		}
		$power = 5;
		$result = 0;
		$num = $n;
		// Execute loop until n is less than 1
		while ($num > 0)
		{
			if (($num & 1) == 1)
			{
				// When last bit are active
				$result += $power;
			}
			// Increase power of 5
			$power *= 5;
			// Divide the number by 2
			$num = intval($num / 2);
		}
		// Display calculated result
		echo " ". $n ."-th magic number is : ". $result ."\n";
	}
}

function main()
{
	$task = new MagicNumber();
	// Test cases
	$task->findNthMagicNo(4);
	$task->findNthMagicNo(7);
	$task->findNthMagicNo(10);
}
main();

Output

 4-th magic number is : 125
 7-th magic number is : 155
 10-th magic number is : 650
/*
  Node Js Program for
  Find nth magic number
*/
class MagicNumber
{
	// Find the nth magic number
	findNthMagicNo(n)
	{
		if (n < 0)
		{
			return;
		}
		var power = 5;
		var result = 0;
		var num = n;
		// Execute loop until n is less than 1
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				// When last bit are active
				result += power;
			}
			// Increase power of 5
			power *= 5;
			// Divide the number by 2
			num = parseInt(num / 2);
		}
		// Display calculated result
		process.stdout.write(" " + n + "-th magic number is : " + result + "\n");
	}
}

function main()
{
	var task = new MagicNumber();
	// Test cases
	task.findNthMagicNo(4);
	task.findNthMagicNo(7);
	task.findNthMagicNo(10);
}
main();

Output

 4-th magic number is : 125
 7-th magic number is : 155
 10-th magic number is : 650
#   Python 3 Program for
#   Find nth magic number

class MagicNumber :
	#  Find the nth magic number
	def findNthMagicNo(self, n) :
		if (n < 0) :
			return
		
		power = 5
		result = 0
		num = n
		#  Execute loop until n is less than 1
		while (num > 0) :
			if ((num & 1) == 1) :
				#  When last bit are active
				result += power
			
			#  Increase power of 5
			power *= 5
			#  Divide the number by 2
			num = int(num / 2)
		
		#  Display calculated result
		print(" ", n ,"-th magic number is : ", result )
	

def main() :
	task = MagicNumber()
	#  Test cases
	task.findNthMagicNo(4)
	task.findNthMagicNo(7)
	task.findNthMagicNo(10)

if __name__ == "__main__": main()

Output

  4 -th magic number is :  125
  7 -th magic number is :  155
  10 -th magic number is :  650
#   Ruby Program for
#   Find nth magic number

class MagicNumber 
	#  Find the nth magic number
	def findNthMagicNo(n) 
		if (n < 0) 
			return
		end

		power = 5
		result = 0
		num = n
		#  Execute loop until n is less than 1
		while (num > 0) 
			if ((num & 1) == 1) 
				#  When last bit are active
				result += power
			end

			#  Increase power of 5
			power *= 5
			#  Divide the number by 2
			num = num / 2
		end

		#  Display calculated result
		print(" ", n ,"-th magic number is : ", result ,"\n")
	end

end

def main() 
	task = MagicNumber.new()
	#  Test cases
	task.findNthMagicNo(4)
	task.findNthMagicNo(7)
	task.findNthMagicNo(10)
end

main()

Output

 4-th magic number is : 125
 7-th magic number is : 155
 10-th magic number is : 650
/*
  Scala Program for
  Find nth magic number
*/
class MagicNumber
{
	// Find the nth magic number
	def findNthMagicNo(n: Int): Unit = {
		if (n < 0)
		{
			return;
		}
		var power: Int = 5;
		var result: Int = 0;
		var num: Int = n;
		// Execute loop until n is less than 1
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				// When last bit are active
				result += power;
			}
			// Increase power of 5
			power *= 5;
			// Divide the number by 2
			num = (num / 2).toInt;
		}
		// Display calculated result
		print(" " + n + "-th magic number is : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: MagicNumber = new MagicNumber();
		// Test cases
		task.findNthMagicNo(4);
		task.findNthMagicNo(7);
		task.findNthMagicNo(10);
	}
}

Output

 4-th magic number is : 125
 7-th magic number is : 155
 10-th magic number is : 650
/*
  Swift 4 Program for
  Find nth magic number
*/
class MagicNumber
{
	// Find the nth magic number
	func findNthMagicNo(_ n: Int)
	{
		if (n < 0)
		{
			return;
		}
		var power: Int = 5;
		var result: Int = 0;
		var num: Int = n;
		// Execute loop until n is less than 1
		while (num > 0)
		{
			if ((num & 1) == 1)
			{
				// When last bit are active
				result += power;
			}
			// Increase power of 5
			power *= 5;
			// Divide the number by 2
			num = num / 2;
		}
		// Display calculated result
		print(" ", n ,"-th magic number is : ", result );
	}
}
func main()
{
	let task: MagicNumber = MagicNumber();
	// Test cases
	task.findNthMagicNo(4);
	task.findNthMagicNo(7);
	task.findNthMagicNo(10);
}
main();

Output

  4 -th magic number is :  125
  7 -th magic number is :  155
  10 -th magic number is :  650
/*
  Kotlin Program for
  Find nth magic number
*/
class MagicNumber
{
	// Find the nth magic number
	fun findNthMagicNo(n: Int): Unit
	{
		if (n < 0)
		{
			return;
		}
		var power: Int = 5;
		var result: Int = 0;
		var num: Int = n;
		// Execute loop until n is less than 1
		while (num > 0)
		{
			if ((num and 1) == 1)
			{
				// When last bit are active
				result += power;
			}
			// Increase power of 5
			power *= 5;
			// Divide the number by 2
			num = num / 2;
		}
		// Display calculated result
		print(" " + n + "-th magic number is : " + result + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: MagicNumber = MagicNumber();
	// Test cases
	task.findNthMagicNo(4);
	task.findNthMagicNo(7);
	task.findNthMagicNo(10);
}

Output

 4-th magic number is : 125
 7-th magic number is : 155
 10-th magic number is : 650


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