c++ - Odd APPCRASH error in GCD program? -
using namespace std; #include <iostream> int gcd(int a, int b){ while(b!=0){ int temp=b; b=a%b; a=temp; } return a; } int main(){ int a=0, b=0; cout<<"please enter 2 integers find gcd using euclidean algorithm."; cin>>a>>b; cout<<"the greatest common divisor of "<<a<<" , "<<b<<" "<<gcd(a,b)<<"."; } this simple c++ program finds greatest common divisor of 2 numbers, , runs fine. however, if change
int gcd(int a, int b){ while(b!=0){ into while(a!=0){, experience appcrash error, exception code c0000094 kills run. running c++11 iso standards , ide code::blocks 16.
you have "division 0" error.
demonstration:
#include <iostream> using namespace std; int gcd(int a, int b) { while (a != 0) { int temp = b; if (b == 0) { cout << "divison 0\n"; return 0; } b = % b; = temp; } return a; } int main() { int = 0, b = 0; cout << "please enter 2 integers find gcd using euclidean algorithm."; cin >> >> b; cout << "the greatest common divisor of " << << " , " << b << " " << gcd(a, b) << "."; } input:
5 5 disclaimer: program demonstrates there "division 0" error, still not correct.
Comments
Post a Comment