Check if a number is power of another number

Here given code implementation process.

//C Program
//Check if a number is power of another number 
#include <stdio.h>

//Calculate power (or exponent) of a number
void is_power(int number,int result)
{
 
  int status = 0;

  int auxiliary = number;
  
  for (int i = 0; auxiliary <= result; ++i)
  {
    if(auxiliary == result)
    {
      //When given power is form of result
      status=1;
      break;
    }
    //Calculate power
    auxiliary *= number;
  }
  if(status==0)
  {
    printf("%d Is not power of %d number \n",number,result );
  }
  else
  {
    printf("%d Is power of %d number \n",number,result );
  }

}
int main()
{
  //Test Case
  is_power(3,27);
  is_power(10,1000);
  is_power(9,180);

  return 0;
}

Output

3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
/*
 C++ Program
 Check if a number is power of another number 
*/
#include<iostream>
using namespace std;

class MyNumber {
	public:

		//Calculate power (or exponent) of a number
		void is_power(int number, int result) {
			int status = 0;
			int auxiliary = number;
			for (int i = 0; auxiliary <= result; ++i) {
				if (auxiliary == result) {
					//When given power is form of result
					status = 1;
					break;
				}
				//Calculate power
				auxiliary *= number;
			}
			if (status == 0) {
				cout << number << " Is not power of " << result << " number \n";
			} else {
				cout << number << " Is power of " << result << " number \n";
			}
		}
};
int main() {
	MyNumber obj;
	//Test case
	obj.is_power(3, 27);
	obj.is_power(10, 1000);
	obj.is_power(9, 180);
	return 0;
}

Output

3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
/*
  Java Program
  Check if a number is power of another number 
*/

public class MyNumber {

  //Calculate power (or exponent) of a number
  void is_power(int number,int result)
  {
   
    int status = 0;

    int auxiliary = number;
    
    for (int i = 0; auxiliary <= result; ++i)
    {
      if(auxiliary == result)
      {
        //When given power is form of result
        status=1;
        break;
      }
      //Calculate power
      auxiliary *= number;
    }
    if(status==0)
    {
      System.out.print(number+" Is not power of "+result+" number \n");
    }
    else
    {
      System.out.print(number+" Is power of "+result+" number \n");
    }

  }

  public static void main(String[] args) {

    MyNumber obj = new MyNumber();
    //Test case
    obj.is_power(3,27);
    obj.is_power(10,1000);
    obj.is_power(9,180);

  }
}

Output

3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
/*
  C# Program
  Check if a number is power of another number 
*/
using System;
public class MyNumber {

	//Calculate power (or exponent) of a number
	void is_power(int number, int result) {

		int status = 0;

		int auxiliary = number;

		for (int i = 0; auxiliary <= result; ++i) {
			if (auxiliary == result) {
				//When given power is form of result
				status = 1;
				break;
			}
			//Calculate power
			auxiliary *= number;
		}
		if (status == 0) {
			Console.Write(number + " Is not power of " + result + " number \n");
		} else {
			Console.Write(number + " Is power of " + result + " number \n");
		}

	}

	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();
		//Test case
		obj.is_power(3, 27);
		obj.is_power(10, 1000);
		obj.is_power(9, 180);

	}
}

Output

3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
# Python 3 Program
# Check if a number is power of another number 
class MyNumber :
	def is_power(self, number, result) :
		status = 0
		auxiliary = number
		i = 0
		while (auxiliary <= result) :
			if (auxiliary == result) :
				#When given power is form of result
				status = 1
				break
			
			#Calculate power
			auxiliary *= number
			i += 1
		
		if (status == 0) :
			print(number ," Is not power of ", result ," number")
		else :
			print(number ," Is power of ", result ," number")
		
	

def main() :
	obj = MyNumber()
	obj.is_power(3, 27)
	obj.is_power(10, 1000)
	obj.is_power(9, 180)


if __name__ == "__main__":
	main()

Output

3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
# Ruby Program 
# Check if a number is power of another number 
class MyNumber 
	def is_power(number, result) 
		status = 0
		auxiliary = number
		i = 0
		while (auxiliary <= result) 
			if (auxiliary == result) 
				#When given power is form of result
				status = 1
				break
			end
			#Calculate power
			auxiliary *= number
			i += 1
		end
		if (status == 0) 
			print(number ," Is not power of ", result ," number \n")
		else 
			print(number ," Is power of ", result ," number \n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	obj.is_power(3, 27)
	obj.is_power(10, 1000)
	obj.is_power(9, 180)
end
main()

Output

3 Is power of 27 number 
10 Is power of 1000 number 
9 Is not power of 180 number 
/*
 Scala Program
 Check if a number is power of another number 
*/
import scala.util.control.Breaks._
class MyNumber {
	def is_power(number: Int, result: Int): Unit = {
		var status: Int = 0;
		var auxiliary: Int = number;
		var i: Int = 0;
		breakable {
			while (auxiliary <= result) {
				if (auxiliary == result) {
					//When given power is form of result
					status = 1;
					break;
				}
				//Calculate power
				auxiliary *= number;
				i += 1;
			}
		}
		if (status == 0) {
			print(s"$number Is not power of $result number \n");
		} else {
			print(s"$number Is power of $result number \n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		obj.is_power(3, 27);
		obj.is_power(10, 1000);
		obj.is_power(9, 180);
	}
}

Output

3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
/*
  Swift 4 Program
  Check if a number is power of another number 
*/
class MyNumber {
	func is_power(_ number: Int, _ result: Int) {
		var status: Int = 0;
		var auxiliary: Int = number;
		var i: Int = 0;
		while (auxiliary <= result) {
			if (auxiliary == result) {
				//When given power is form of result
				status = 1;
				break;
			}
			//Calculate power
			auxiliary *= number;
			i += 1;
		}
		if (status == 0) {
			print(number ," Is not power of ", result ," number");
		} else {
			print(number ," Is power of ", result ," number");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	obj.is_power(3, 27);
	obj.is_power(10, 1000);
	obj.is_power(9, 180);
}
main();

Output

3  Is power of  27  number
10  Is power of  1000  number
9  Is not power of  180  number
<?php
/*
  Php Program
  Check if a number is power of another number 
*/
class MyNumber {
	//Calculate power (or exponent) of a number
	function is_power($number, $result) {
		$status = 0;
		$auxiliary = $number;
		for ($i = 0; $auxiliary <= $result; ++$i) {
			if ($auxiliary == $result) {
				//When given power is form of result
				$status = 1;
				break;
			}
			//Calculate power
			$auxiliary *= $number;
		}
		if ($status == 0) {
			echo($number ." Is not power of ". $result ." number \n");
		} else {
			echo($number ." Is power of ". $result ." number \n");
		}
	}
};

function main() {
	$obj = new MyNumber();
	//Test case

	$obj->is_power(3, 27);
	$obj->is_power(10, 1000);
	$obj->is_power(9, 180);
}
main();

Output

3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number
/*
 Node Js Program
 Check if a number is power of another number 
*/
class MyNumber {
	//Calculate power (or exponent) of a number
	is_power(number, result) {
		var status = 0;
		var auxiliary = number;
		for (var i = 0; auxiliary <= result; ++i) {
			if (auxiliary == result) {
				//When given power is form of result
				status = 1;
				break;
			}
			//Calculate power
			auxiliary *= number;
		}
		if (status == 0) {
			process.stdout.write(number + " Is not power of " + result + " number \n");
		} else {
			process.stdout.write(number + " Is power of " + result + " number \n");
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test case
	obj.is_power(3, 27);
	obj.is_power(10, 1000);
	obj.is_power(9, 180)
}
main();

Output

3 Is power of 27 number
10 Is power of 1000 number
9 Is not power of 180 number


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