Advertisement
filomancio

GCD and LCM

Mar 3rd, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int GreatestCommonDivisor(int x,int y);
  6. int LeastCommonMultiple(int x,int y);
  7. void Menu();
  8.  
  9. int main ()
  10. {
  11.     int a,b,c;
  12.     do
  13.     {
  14.         Menu();
  15.         cin>>c;
  16.         system("CLS");
  17.         switch (c)
  18.         {
  19.             case 1:
  20.                 cout<<"Insert New Numbers"<<endl;
  21.                 cin>>a>>b;
  22.                 break;
  23.             case 2:
  24.                 cout<<"Lcm="<<LeastCommonMultiple(a,b)<<endl;
  25.                 break;
  26.             case 3:
  27.                 cout<<"Gcd="<<GreatestCommonDivisor(a,b)<<endl;
  28.                 break;
  29.             case 4:
  30.                 cout<<"The Program Is Closing"<<endl;
  31.         system("PAUSE");
  32.                 break;
  33.             default:
  34.                 cout<<"Wrong Character"<<endl;
  35.         break;
  36.         }
  37.     }while (c!=4);
  38.     return 0;
  39. }
  40.  
  41. int GreatestCommonDivisor(int x,int y)
  42. {
  43.     int m;
  44.     while (y!=0)
  45.     {
  46.         m=x%y;
  47.         x=y;
  48.         y=m;
  49.     }
  50.     return x;
  51. }
  52.  
  53. int LeastCommonMultiple(int x,int y)
  54. {
  55.     int lcm;
  56.     lcm=x*y;
  57.     lcm=lcm/GreatestCommonDivisor(x,y);
  58.     return lcm;
  59. }
  60.  
  61. void Menu()
  62. {
  63.     cout<<"Menu:"<<endl
  64.         <<"1. New numbers."<<endl
  65.         <<"2. Calculation of lcm."<<endl
  66.         <<"3. Calculation of gcd."<<endl
  67.         <<"4. Exit."<<endl;
  68.     return;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement