Sum of the series Kn + ( K ( n | CPP program of above approach ; Recursive C program to compute modular power ; Base cases ; If B is even ; If B is odd ; Function to return sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int exponent ( int A , int B ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; long y ; if ( B % 2 == 0 ) { y = exponent ( A , B / 2 ) ; y = ( y * y ) ; } else { y = A ; y = ( y * exponent ( A , B - 1 ) ) ; } return y ; } int sum ( int k , int n ) { int sum = exponent ( k , n + 1 ) - exponent ( k - 1 , n + 1 ) ; return sum ; } int main ( ) { int n = 3 ; int K = 3 ; cout << sum ( K , n ) ; }