Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Calcolo del MCD con il metodo delle divisioni successive
- //
- // Created by Gianluca Tararbra on 22/02/2018.
- // Copyright © 2018 Gianluca Tararbra. All rights reserved.
- //
- #include <iostream>
- using namespace std;
- int main(int argc, const char * argv[]) {
- int a,b,temp,r,mcd=0;
- //INPUT
- cout<<"a?";
- cin>>a;
- cout<<"b?";
- cin>>b;
- //SCAMBIA VALORI DI a E b SE a<b
- if(a<b)
- {
- temp=b;
- b = a;
- a = temp;
- }
- if(b!=0) //SE B E' DIVERSO DA ZERO CALCOLA MCD CON IL METODO DELLE DIVISIONI SUCCESSIVE
- {
- do{
- r = a%b; //RESTO
- if(r==0) // SE a È DIVISIBILE PER b allora MCD = b
- {
- mcd=b;
- }
- else //ALTRIMENTI SE NON È DIVISIBILE DIVIDI B CON IL RESTO
- {
- a = b;
- b = r;
- }
- }while(r!=0); //RIPETI FINO A CHE IL RESTO NON È UGUALE A ZERO
- }
- else //ALTRIMENTI SE b=0 MCD = a
- {
- mcd=a;
- }
- //OUTPUT
- cout<<mcd<<endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment