Find remainder when a number A raised to N factorial is divided by P | Java program for the above approach ; Function to calculate factorial ; Calculating factorial ; Returning factorial ; Function to calculate resultant remainder ; Function call to calculate factorial of n ; Calculating remainder ; Returning resultant remainder ; Driver Code ; Given Input ; Function Call
import java . lang . * ; import java . util . * ; class GFG { static int fact ( int n ) { int ans = 1 ; for ( int i = 2 ; i <= n ; i ++ ) ans *= i ; return ans ; } static int remainder ( int n , int a , int p ) { int len = fact ( n ) ; int ans = 1 ; for ( int i = 1 ; i <= len ; i ++ ) ans = ( ans * a ) % p ; return ans ; } public static void main ( String args [ ] ) { int A = 2 , N = 1 , P = 2 ; System . out . println ( remainder ( N , A , P ) ) ; } }