Problem 20

Statement
n! means n × (n − 1) × … × 3 × 2 × 1

Find the sum of the digits in the number 100!

Solution
The code, in C.

#include <stdio.h>

#define SIZE 500

void fact(int f,int a[],int n)
{
  int i,j;
  a[0]=1;
  for(i=1;i<=f;i++){
    for(j=0;j<n;j++)
      a[j]*=i;
    for(j=0;j<n-1;j++){
      a[j+1]+=a[j]/10;
      a[j]%=10;
    }
  }
}

int sum(int a[],int n)
{
  int i,s=0;
  for(i=n-1;i>=0;i--)
    s+=a[i];
  return s;
}

int main(void)
{
  int a[SIZE]={0};
  fact(100,a,SIZE);
  printf("%d\n",sum(a,SIZE));
  return 0;
}

Answer
648

No tags for this post.

SPEAK / ADD YOUR COMMENT
Comments are moderated.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>