begin process at 2012 05 27 21:09:11
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Maths & Algorithmes

 > TROUVER LE PGCD DE 2 NOMBRES

TROUVER LE PGCD DE 2 NOMBRES


 Information sur la source

 Description

Voila ce code permet de trouver le pgcd de deux nombre.
faites moi signes si vous avez des commentaire ;)

Source

  • //INCLUDES
  • #include <iostream>
  • using namespace std;
  • //PROTOTYPES
  • int swap(int *a, int *b);
  • //MAIN
  • int main()
  • {
  • int a;
  • int b;
  • int resultat;
  • int temp2;
  • bool vf=true;
  • cin>>a;
  • cin>>b;
  • //Pour que a soit supérieur à b:
  • if(a>b)
  • {
  • cout<<"a est superieur a b"<<endl;
  • }
  • else
  • {
  • cout<<"b est superieure a a"<<endl;
  • swap(&a,&b);
  • }
  • while(vf)
  • {
  • resultat=a-b;
  • if(resultat<=b)
  • {
  • a=b;
  • b=resultat;
  • }
  • if(resultat>b)
  • {
  • a=resultat;
  • }
  • if(resultat==0)
  • {
  • vf=false;
  • cout<<"le pgcd est :"<<temp2<<endl;
  • }
  • temp2=resultat;
  • }
  • int fin;
  • cin>>fin;
  • return 0;
  • }
  • //FONCTIONS
  • int swap(int *a, int *b)
  • {
  • int temp;
  • temp=*a;
  • *a=*b;
  • *b=temp;
  • }
//INCLUDES
#include <iostream>
using namespace std;

//PROTOTYPES
int swap(int *a, int *b);


//MAIN
int main()
{
  
    int a;   
    int b; 
    int resultat;
    int temp2;
    bool vf=true;         

    cin>>a;
    cin>>b;    
    
   
    //Pour que a soit supérieur à b:
    if(a>b)
    {
        cout<<"a est superieur a b"<<endl;
    }
    
    else
    {
        cout<<"b est superieure a a"<<endl;
        swap(&a,&b);
    }
    
    while(vf)
    {
        resultat=a-b;
       
        
                
        if(resultat<=b)
        {
                a=b;
                b=resultat;
        }
        if(resultat>b)
        {
                a=resultat;
        }
        
        if(resultat==0)
        {
                vf=false;
                cout<<"le pgcd est :"<<temp2<<endl;
        }
        temp2=resultat;
    }
        
        
    
    
    
    
    
    
    int fin;
    cin>>fin;
    
return 0;
}            

//FONCTIONS

int swap(int *a, int *b)
{
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}
        



 Sources du même auteur

Source avec Zip Source avec une capture CRYPTER UN FICHIER TXT (WIN32 DEV-CPP)
Source avec Zip LIRE DANS UN FICHIER TEXTE ET TROUVER UN MOT DONNÉ DANS CE F...
Source avec Zip Source avec une capture SDL : FAIRE BOUGER UNE IMAGE AVEC LES TOUCHES ZSQD (POUR APP...
LES SUITES RÉCURRENTES (RÉSOLUTION)
POLYNOME DU SECOND DEGRE EN C++ (COMMENTÉ)

 Sources de la même categorie

Source avec Zip UN EXAMPLE D'APPLICATION EN CUDA DE L'ALGORITHME DE SCAN POU... par oguzaras
Source avec Zip Source avec une capture CHIFFREMENT DE VIGENERE par lajouad
Source avec Zip Source avec une capture ANALYSE SYNTAXIQUE par lajouad
Source avec Zip Source avec une capture STRUCTURE D'UNE MATRICE PAR LES LISTE LINÉAIRE (NON CONTUGUS... par benzarabel
Source avec Zip Source avec une capture DESSINER UNE ARBRE BINAIRE( MODE CONSOLE): par benzarabel

Commentaires et avis

Commentaire de didier1809 le 22/12/2003 10:49:48

ca me parait bien compliqué ce code...

comme ça, ce ne serai pas plus simple ??
(il y à un test à la saisie pour que 'a' ou 'b' soient &gt; à '0')

int fPgcd(int a,int b){
int r;
while(a%b){
r=a%b;
a=b;
b=r;
}
return b;
}

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,359 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales