Guest User

Untitled

a guest
Mar 6th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // Calcolo del MCD con il metodo delle divisioni successive
  4. //
  5. // Created by Gianluca Tararbra on 22/02/2018.
  6. // Copyright © 2018 Gianluca Tararbra. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. using namespace std;
  11. int main(int argc, const char * argv[]) {
  12. int a,b,temp,r,mcd=0;
  13. //INPUT
  14. cout<<"a?";
  15. cin>>a;
  16. cout<<"b?";
  17. cin>>b;
  18. //SCAMBIA VALORI DI a E b SE a<b
  19. if(a<b)
  20. {
  21. temp=b;
  22. b = a;
  23. a = temp;
  24. }
  25.  
  26. if(b!=0) //SE B E' DIVERSO DA ZERO CALCOLA MCD CON IL METODO DELLE DIVISIONI SUCCESSIVE
  27. {
  28. do{
  29. r = a%b; //RESTO
  30. if(r==0) // SE a È DIVISIBILE PER b allora MCD = b
  31. {
  32. mcd=b;
  33. }
  34. else //ALTRIMENTI SE NON È DIVISIBILE DIVIDI B CON IL RESTO
  35. {
  36. a = b;
  37. b = r;
  38. }
  39.  
  40. }while(r!=0); //RIPETI FINO A CHE IL RESTO NON È UGUALE A ZERO
  41. }
  42. else //ALTRIMENTI SE b=0 MCD = a
  43. {
  44. mcd=a;
  45. }
  46. //OUTPUT
  47. cout<<mcd<<endl;
  48. return 0;
  49. }
Add Comment
Please, Sign In to add comment