ProjectEuler Problem-20 Solution

Problem-20

n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!

Solution

#include<iostream>
using namespace std;

int main()
{
 int arr[200]={0},N,j,i,sum;
 cout<<"Enter the Number :";
 cin>>N;
 arr[0]=1;
 for(j=N;j>=2;j--)
 {
  for(i=0;i<=N+60;i++)
     arr[i]*=j;
     for(i=0;i<=N+60;i++)
     {
      arr[i+1]+=arr[i]/10;
      arr[i]%=10;
     }
 }
 cout<<"Factorial :"<<endl;
 sum=0;
 for(i=N+60;i>=0;i--)
 {
  cout<<arr[i]; 
  sum+=arr[i];
    }
 cout<<endl<<"Sum of Digits :"<<sum;
 return 0;
}


No comments: