Given a number n , find the first k digits of n ^ n | Java program to find the first k digits of n ^ n ; function that manually calculates n ^ n and then removes digits until k digits remain ; loop will terminate when there are only k digits left ; Driver code
public class Digits { static long firstkdigits ( int n , int k ) { long product = 1 ; for ( int i = 0 ; i < n ; i ++ ) product *= n ; while ( ( int ) ( product / Math . pow ( 10 , k ) ) != 0 ) product = product / 10 ; return product ; } public static void main ( String [ ] args ) { int n = 15 ; int k = 4 ; System . out . println ( firstkdigits ( n , k ) ) ; } }