Traduction en C#
Calcul du pgcd de 2 entiers (méthode Euclide)
Une classe C# solution du problème :
 
using System;
namespace CsExosAlgo1
{
class ApplicationEuclide {
 
static void Main (string[ ] args) {
       System.Console.Write("Entrez le premier nombre : ");
       p  = Int32.Parse( System.Console.ReadLine( ) ) ;
       System.Console.Write("Entrez le deuxième nombre : ");
       q  = Int32.Parse( System.Console.ReadLine( ) ) ;
      if (p*q!=0)
        System.Console.WriteLine("Le pgcd de "+p+" et de "+q+" est "+pgcd(p,q));
      else
      System.Console.WriteLine("Le pgcd n'existe pas lorsque l'un des deux nombres est nul !");
  }

 
static int pgcd (int a , int b) {
    if ( b>a) {
            t = a;
            a = b;
            b = t;
     }
    do {
            r = a % b;
            a = b;
            b = r;
    } while(r !=0);
   return a ;
  }
}
}

Source recopiable (cliquez sur le lien)

Remonter