Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.util.Scanner; public class Root{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); int a = sc. nextInt (); int n = sc. nextInt (); System. out . println (nthRoot(n,a)); } public static int nthRoot ( int n, int a){ for ( int i= 1 ; i<=Math. sqrt (a); i++){ if (a==( int )Math. pow (i,n)) return i; } return - 1 ; } } Python import math def nthRoot(n,a): for i in range ( 1 , int (math . sqrt(a) + 1 )): if math . pow(i, n) == a: return i return - 1 a = int ( input ( "Enter n:" )) n = int ( input ( "Enter a:" )) print (nthRoot(n,a))
This blog contains coding/programming and interview questions of MNCs like TCS, Wipro, Infosys, Capgemini, Mindtree, Amazon, Google, Facebook, Flipkart, Cisco, HP, Dell, etc. with placement preparation guides.