By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

I need some help, Please!

P: 8
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int GCD(int a, int b)
  5. {
  6.    while(1)
  7.    {
  8.       a = a % b;
  9.          if(a ==0)
  10.             return b;
  11.          b = b % a;
  12.       if(b == 0)
  13.          return a;
  14.    }
  15. }
  16.  
  17. int LCM(int x, int y)
  18. {
  19.    int i;
  20.    if (x > Y)
  21.       for(i = y; i <= x * y; i++)
  22.       {
  23.          if (i % x == 0 && i % y ==0)
  24.             return i;
  25.       }
  26.    else
  27.       for(i = y; i <= x * y; i++)
  28.       {
  29.          if(i % x == 0 && i % y ==0)
  30.             return i;
  31.       }
  32.    return i;
  33. }
  34.  
  35. int main()
  36. {
  37.    char cAgain;
  38.    int x, y;
  39.  
  40.    do
  41.    {
  42.       cout << endl << "Please enter value one: " << endl << endl;
  43.       cin >> x;
  44.       cout << endl << "Please enter value two: " << endl << endl;
  45.       cin >> y;
  46.       cout << endl << "The Greatest Commin Factor of "
  47.            << x << " and " << y << " is " << GCD(x,y) << endl;
  48.       cout << endl << "The Least Common Multiple of " 
  49.            << x << " and " << y << " is " << LCM(x,y) << endl;
  50.       cout << "Would you like to calculate another? Y/y/N/n"
  51.            << endl << endl;
  52.       cin >> cAgain;
  53.    }while(cAgain == 'Y' || cAgain == 'y');
  54.  
  55.    cout << endl << "Thank you for using this program." << endl;
  56.  
  57.    return 0;
  58.  
  59. }
There is my code. And it works except when i use the numbers -10 and zero in that order. The error says Floating Point Exception and just stops. So I was wondering what that means and how do I fix it in my code. Thanks
Mar 19 '07 #1
Share this Question
Share on Google+
3 Replies


Ganon11
Expert 2.5K+
P: 3,652
Does this problem occur every time you include a 0, or only when you include a 0 as the second number, or only when you include -10? I suspect the error comes when you try and use modulus division by 0, but I can't be sure.
Mar 19 '07 #2

P: 8
Does this problem occur every time you include a 0, or only when you include a 0 as the second number, or only when you include -10? I suspect the error comes when you try and use modulus division by 0, but I can't be sure.
It happens everytime the 0 is the second number.
Mar 20 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
Probably division by zero, then. You should add a message informing the user that he/she cannot enter 0. If you like, you can also add some error trapping that will prevent the user from entering 0.
Mar 20 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.