1

how to self-detect when it has overflowed the value (32 bit signed integer) of an int variable.

Can I add some conditional logic (an if statement) that breaks the loop when overflow is detected.

#include<iostream>
using namespace std;

int factorial(int n){
   if (n== 1){
       return 1;
   }
    else {
       return n * factorial(n-1);
    }


}
int main() {

for (int i = 0; i < 20; i++) {

   cout << i+1 <<"!:" << factorial(i+1) << endl;
 }

   system("pause");
}
user3602755
  • 27
  • 1
  • 6

0 Answers0