Skip to main content

Find nth magic number

Find nth magic number in java

/*
  Java program for
  Find the 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.println(" " + n + "-th magic number is : " + result );
	}
	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

Find nth magic number in c++

// Include header file
#include <iostream>

//  Stdc++11 program for
//  Find the nth magic number
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
        std::cout << " " << n 
                  << "-th magic number is : " 
                  << result << std::endl;
    }
};
int main(int argc, char **argv){
	MagicNumber task;
	// 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

Find nth magic number in c

// Include header file
#include <stdio.h>
//  C program for
//  Find the nth magic number

// Find the value 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

Find nth magic number in golang

package main
import "fmt"
//  Golang program for
//  Find the nth magic number
// Find the value of 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
	for (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
	fmt.Printf(" %d-th magic number is : %d\n", n, result);
}
func main() {
	// Test cases
	findNthMagicNo(4);
	findNthMagicNo(7);
	findNthMagicNo(10);
}
Output
 4-th magic number is : 125
 7-th magic number is : 155
 10-th magic number is : 650

Find nth magic number in c#

// Include namespace system
using System; 
//  C# program for
//  Find the nth magic number
public class MagicNumber
{
    // Find the value of nth magic number
    public void findNthMagicNo(int 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 = (int)(num / 2);
        }
        // Display calculated result
        Console.WriteLine(" " + n + "-th magic number is : " + result);
    }
    public static void Main(String[] args)
    {
        var 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

Find nth magic number in php

<?php 
//  Php program for
//  Find the nth magic number
class MagicNumber
{
    // Find the value of nth magic number
    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 = (int)($num / 2);
        }
        // Display calculated result
        echo " $n-th magic number is : $result\n";
    }
}
$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

Find nth magic number in node js

//  Node Js program for
//  Find the nth magic number
class MagicNumber
{
    // Find the value of 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
        console.log(" " + n + "-th magic number is : " + result);
    }
}
 // Start program execution
var 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

Find nth magic number in typescript

//  Typescript program for
//  Find the nth magic number
class MagicNumber
{
    // Find the value of nth magic number
    public  findNthMagicNo(n:number)
    {
        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
        console.log(" " + n + "-th magic number is : " + result);
    }

}
var task = new MagicNumber();
// Test cases
task.findNthMagicNo(4);
task.findNthMagicNo(7);
task.findNthMagicNo(10);
/*
 file : code.ts
 tsc --target es6 code.ts
 node code.js
 */
Output
 4-th magic number is : 125
 7-th magic number is : 155
 10-th magic number is : 650

Find nth magic number in python

#  Python 3 program for
#  Find the nth magic number
class MagicNumber :
    # Find the value of 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,sep="")
  

if __name__=="__main__":
    task = 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

Find nth magic number in ruby

#  Ruby program for
#  Find the nth magic number
class MagicNumber
    # Find the value of 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.to_s + "-th magic number is : " + 
            result.to_s,"\n")
    end
end
task = MagicNumber.new()
# 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

Find nth magic number in scala

//  Scala program for
//  Find the nth magic number
class MagicNumber ()
{
    // Find the value of nth magic number
    def findNthMagicNo(n : Int) : Unit=
    {
        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 = num / 2
        }
        // Display calculated result
        println(" " + n + "-th magic number is : " + result)
    }
}

object Main 
{
	def main(args : Array[String]) : Unit=
    {
        var 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

Find nth magic number in swift

import Foundation
//  Swift program for
//  Find the nth magic number
class MagicNumber
{
    // Find the value of 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(" " + String(n) + "-th magic number is : " + String(result));
    }
}

let 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

Find nth magic number in kotlin

//  Kotlin program for
//  Find the nth magic number
class MagicNumber {
    // Find the value of 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
        println(" " + n.toString() + 
                "-th magic number is : " + result.toString());
    }
}
fun main(args : Array<String>) : Unit
{
    val 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




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