title: How to handle integer overflow in C
body: <p>I'm trying to create anagrams from words where answers are supposed to be for example:</p>
  
  <p>The word "at" should have two anagrams.
  ordeals should have 5040 anagrams.
  abcdABCDabcd shoud have 29937600 anagrams.
  abcdefghijklmnopqrstuvwxyz should have 403291461126605635584000000 anagrams.
  abcdefghijklmabcdefghijklm should have 49229914688306352000000. </p>
  
  <p>My program seems to work for the first three examples but not for the last two. How can I change the program to make it work?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;memory.h&gt;
  
  int contains(char haystack[], char needle) {
      size_t len = strlen(haystack);
      int i;
      for (i = 0; i &lt; len; i++) {
          if (haystack[i] == needle) {
              return 1;
          }
      }
      return 0;
  }
  
  unsigned long long int factorial(unsigned long long int f) {
      if (f == 0)
          return 1;
      return (f * factorial(f - 1));
  }
  
  int main(void) {
      char str[1000], ch;
      unsigned long long int i;
      unsigned long long int frequency = 0;
      float answer = 0;
      char visited[1000];
      int indexvisited = 0;
      printf("Enter a string: ");
      scanf("%s", str);
      for (i = 0; str[i] != '\0'; ++i);
      unsigned long long int nominator = 1;
      for (int j = 0; j &lt; i; ++j) {
          ch = str[j];
          frequency = 0;
          if (!contains(visited, ch)) {
              for (int k = 0; str[k] != '\0'; ++k) {
                  if (ch == str[k])
                      ++frequency;
              }
              printf("Frequency of %c = %lld\n", ch, frequency);
              visited[indexvisited] = ch;
              visited[++indexvisited] = '\0';
              nominator = nominator * factorial(frequency);
          }
      }
      printf("Number of anagrams = %llu\n", (factorial( i )/nominator ) );
      return 0;
  
  }
  </code></pre>