Find sum of digits in factorial of a number | C ++ program to find sum of digits in factorial of a number ; Function to multiply x with large number stored in vector v . Result is stored in v . ; Calculate res + prev carry ; updation at ith position ; Returns sum of digits in n ! ; One by one multiply i to current vector and update the vector . ; Find sum of digits in vector v [ ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void multiply ( vector < int > & v , int x ) { int carry = 0 , res ; int size = v . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) { int res = carry + v [ i ] * x ; v [ i ] = res % 10 ; carry = res / 10 ; } while ( carry != 0 ) { v . push_back ( carry % 10 ) ; carry /= 10 ; } } int findSumOfDigits ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) multiply ( v , i ) ; int sum = 0 ; int size = v . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) sum += v [ i ] ; return sum ; } int main ( ) { int n = 1000 ; cout << findSumOfDigits ( n ) ; return 0 ; }