fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. void fibonacci(unsigned int n);
  6.  
  7. int main() {
  8. int n = 93;
  9. fibonacci(n);
  10.  
  11. return 0;
  12. }
  13.  
  14. void fibonacci(unsigned int n) {
  15.  
  16. //n_minus_1 represents what's supposed to be F(n-1)
  17. //n_minus_2 represents what's supposed to be F(n-2)
  18. //sum represents F(n) which equals F(n-1)+F(n-2)
  19. unsigned long long n_minus_1 = 0, n_minus_2 = 1;
  20. char sumCh;
  21. unsigned char sumUCh;
  22. short sumSh;
  23. unsigned short sumUSh;
  24. int sumInt;
  25. unsigned int sumUInt;
  26. long sumL;
  27. unsigned long sumUL;
  28. long long sumLL;
  29. unsigned long long sumULL;
  30.  
  31. switch (n) {
  32. case 0:
  33. sumCh = 0;
  34. sumUCh = 0;
  35. sumSh = 0;
  36. sumUSh = 0;
  37. sumInt = 0;
  38. sumUInt = 0;
  39. sumL = 0;
  40. sumUL = 0;
  41. sumLL = 0;
  42. sumULL = 0;
  43.  
  44. case 1:
  45. sumCh = 1;
  46. sumUCh = 1;
  47. sumSh = 1;
  48. sumUSh = 1;
  49. sumInt = 1;
  50. sumUInt = 1;
  51. sumL = 1;
  52. sumUL = 1;
  53. sumLL = 1;
  54. sumULL = 1;
  55. //otherwise the sum (i.e F(n)) is equal to (n_minus_1)+(n_minus_2) (i.e F(n-1)+F(n-2)
  56. //the counter keeps on moving the variables areound 'n' times
  57. //so that on each time F(n-2) becomes equal to F(n-1) and F(n-1) becomes equal to F(n)
  58. //and F(n) is reevaluated each time in the loop
  59. default:
  60. for (int counter = 0; counter < n; counter++) {
  61. sumCh = n_minus_1 + n_minus_2;
  62. sumUCh = n_minus_1 + n_minus_2;
  63. sumSh = n_minus_1 + n_minus_2;
  64. sumUSh = n_minus_1 + n_minus_2;
  65. sumInt = n_minus_1 + n_minus_2;
  66. sumUInt = n_minus_1 + n_minus_2;
  67. sumL = n_minus_1 + n_minus_2;
  68. sumUL = n_minus_1 + n_minus_2;
  69. sumLL = n_minus_1 + n_minus_2;
  70. sumULL = n_minus_1 + n_minus_2;
  71. n_minus_2 = n_minus_1;
  72. n_minus_1 = sumULL;
  73. }
  74. }
  75. cout << "The fibonacci number (char) is " << (int)sumCh << endl;
  76. cout << "Overflow in n=12" << endl;
  77. cout << "The fibonacci number (unsigned char) is " << (int)sumUCh << endl;
  78. cout << "Overflow in n=14" << endl;
  79. cout << "The fibonacci number (short) is " << sumSh << endl;
  80. cout << "Overflow in n=24" << endl;
  81. cout << "The fibonacci number (unsigned short) is " << sumUSh << endl;
  82. cout << "Overflow in n=25" << endl;
  83. cout << "The fibonacci number (int) is " << sumInt << endl;
  84. cout << "Overflow in n=47" << endl;
  85. cout << "The fibonacci number (unsigned int) is " << sumUInt << endl;
  86. cout << "Overflow in n=48" << endl;
  87. cout << "The fibonacci number (long) is " << sumL << endl;
  88. cout << "Overflow (NetBeans) in n=47" << endl;
  89. cout << "The fibonacci number (unsigned long) is " << sumUL << endl;
  90. cout << "Overflow (NetBeans) in n=48" << endl;
  91. cout << "The fibonacci number (long long) is " << sumLL << endl;
  92. cout << "Overflow (Netbeans) in n=93" << endl;
  93. cout << "The fibonacci number (unsigned long long) is " << sumULL << endl;
  94. cout << "Overflow (Netbeans) in n=94" << endl;
  95. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
The fibonacci number (char) is 2
Overflow in n=12
The fibonacci number (unsigned char) is 2
Overflow in n=14
The fibonacci number (short) is 9986
Overflow in n=24
The fibonacci number (unsigned short) is 9986
Overflow in n=25
The fibonacci number (int) is 572466946
Overflow in n=47
The fibonacci number (unsigned int) is 572466946
Overflow in n=48
The fibonacci number (long) is -6246583658587674878
Overflow (NetBeans) in n=47
The fibonacci number (unsigned long) is 12200160415121876738
Overflow (NetBeans) in n=48
The fibonacci number (long long) is -6246583658587674878
Overflow (Netbeans) in n=93
The fibonacci number (unsigned long long) is 12200160415121876738
Overflow (Netbeans) in n=94