Given a number n , find the first k digits of n ^ n | C ++ 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 function
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned long long firstkdigits ( int n , int k ) { unsigned long long product = 1 ; for ( int i = 0 ; i < n ; i ++ ) product *= n ; while ( ( int ) ( product / pow ( 10 , k ) ) != 0 ) product = product / 10 ; return product ; } int main ( ) { int n = 15 ; int k = 4 ; cout << firstkdigits ( n , k ) ; return 0 ; }