title: C print first million Fibonacci numbers
body: <p>I am trying to write C code which will print the first 1million Fibonacci numbers.</p>
  <p>The actual problem is I want to get the <strong>last 10 digits of F(1,000,000)</strong></p>
  <p>I understand how the sequence works and how to write the code to achieve that however as <code>F(1,000,000)</code> is very large I am struggling to find a way to represent it.</p>
  <p>This is code I am using:</p>
  <pre><code>#include&lt;stdio.h&gt;
   
  int main()
  {
     unsigned long long n, first = 0, second = 1, next, c;
   
     printf(&quot;Enter the number of terms\n&quot;);
     scanf(&quot;%d&quot;,&amp;n);
   
     printf(&quot;First %d terms of Fibonacci series are :-\n&quot;,n);
   
     for ( c = 0 ; c &lt; n ; c++ )
     {
        if ( c &lt;= 1 )
           next = c;
        else
        {
           next = first + second;
           first = second;
           second = next;
        }
        printf(&quot;%d\n&quot;,next);
     }
   
     return 0;
  }
  </code></pre>
  <p>I am using <code>long long</code> to try and make sure there are enough bits to store the number.</p>
  <p>This is the output for the first <code>100</code> numbers:</p>
  <pre><code>First 100 terms of Fibonacci series are :-                                                                                                                                     
  
      0                                                                                                                                                                              
      1                                                                                                                                                                              
      1                                                                                                                                                                              
      2                                                                                                                                                                              
      3                                                                                                                                                                              
      5                                                                                                                                                                              
      8                                                                                                                                                                              
      13                                                                                                                                                                             
      21                                                                                                                                                                             
      34                                                                                                                                                                             
      55                                                                                                                                                                             
      89                                                                                                                                                                             
      144                                                                                                                                                                            
      233                                                                                                                                                                            
      377                                                                                                                                                                            
      610                                                                                                                                                                            
      987                                                                                                                                                                            
      1597                                                                                                                                                                           
      2584                                                                                                                                                                           
      4181                                                                                                                                                                           
      6765                                                                                                                                                                           
      10946                                                                                                                                                                          
      17711                                                                                                                                                                          
      28657                                                                                                                                                                          
      46368                                                                                                                                                                          
      75025                                                                                                                                                                          
      121393                                                                                                                                                                         
      196418                                                                                                                                                                         
      317811                                                                                                                                                                         
      514229                                                                                                                                                                         
      832040                                                                                                                                                                         
      1346269                                                                                                                                                                        
      2178309                                                                                                                                                                        
      3524578                                                                                                                                                                        
      5702887                                                                                                                                                                        
      9227465                                                                                                                                                                        
      14930352                                                                                                                                                                       
      24157817                                                                                                                                                                       
      39088169                                                                                                                                                                       
      63245986
      102334155                                                                                                                                                                      
      165580141                                                                                                                                                                      
      267914296                                                                                                                                                                      
      433494437                                                                                                                                                                      
      701408733                                                                                                                                                                      
      1134903170                                                                                                                                                                     
      1836311903                                                                                                                                                                     
      -1323752223                                                                                                                                                                    
      512559680                                                                                                                                                                      
      -811192543                                                                                                                                                                     
      -298632863                                                                                                                                                                     
      -1109825406                                                                                                                                                                    
      -1408458269
      ...
  </code></pre>
  <p>Truncated the output but you can see the problem, I believe the size of the number generated is causing the value to overflow to negative. I don't understand how to stop it in all honesty.</p>
  <p>Can anybody point me in the right direction to how to actually handle numbers of this size?</p>
  <p>I haven't tried to print the first million because if it fails on printing <code>F(100)</code> there isn't much hope of it printing <code>F(1,000,000)</code>.</p>