Find the unit place digit of sum of N factorials | C ++ program to find the unit place digit of the first N natural numbers factorials ; Function to find the unit 's place digit ; Let us write for cases when N is smaller than or equal to 4. ; We know following ( 1 ! + 2 ! + 3 ! + 4 ! ) % 10 = 3 else ( N >= 4 ) ; Driver code
#include <iostream> NEW_LINE using namespace std ; int get_unit_digit ( long long int N ) { if ( N == 0 N == 1 ) return 1 ; else if ( N == 2 ) return 3 ; else if ( N == 3 ) return 9 ; return 3 ; } int main ( ) { long long int N = 1 ; for ( N = 0 ; N <= 10 ; N ++ ) cout << " For ▁ N ▁ = ▁ " << N << " ▁ : ▁ " << get_unit_digit ( N ) << endl ; return 0 ; }