C language to achieve the greatest common divisor of two numbers

vs2019

#include<stdio.h>

int G_cd(int a, int b)
{
 for (int i = a < b ? a : b; i >=0 ; i--)
 {
  if (a % i == 0 && b % i == 0)
  {
   return i;
  }
 }
 }

int main()
{
 printf("Please input a and b:\n");
 int a = 0;
 int b = 0;
 scanf_s("%d %d", &a, &b);
 int Max = G_cd(a,b);
 printf("%d 和 %d 的最大公约数是:%d\n", a, b, Max);
 return 0;
}

The results show the code

Published 15 original articles · won praise 0 · Views 225

Guess you like

Origin blog.csdn.net/qq_44423388/article/details/104281302