#include <stdio.h>
unsigned int least_common_factor(unsigned int var_a, unsigned int var_b)
{
unsigned int i, total;
total = 1;
while ((var_a > 1) && (var_b > 1))
{
for (i = 2; i <= ((var_a < var_b) ? var_a : var_b); i++)
{
if (((var_a % i) == 0) && ((var_b % i) == 0))
{
total *= i;
var_a /= i;
var_b /= i;
break;
}
}
if (i >= ((var_a < var_b) ? var_a : var_b))
break;
}
// return total * var_a * var_b; //greatest common multiple
return total; // least common factor
}
int main()
{
unsigned int var_a;
unsigned int var_b;
printf("\nPlease input two value:");
scanf("%d %d", &var_a, &var_b);
printf("\n=============================================\n");
printf("a = %d, b = %d, and least_common_factor is %d", var_a, var_b, least_common_factor(var_a, var_b));
printf(", and greatest common multiple is %d", var_a * var_b / least_common_factor(var_a, var_b));
printf("\n=============================================\n");
return 0;
}
Please input two value:15 18
=============================================
a = 15, b = 18, and least_common_factor is 3, and greatest common multiple is 90
=============================================
Please input two value:15 18
=============================================
a = 15, b = 18, and least_common_factor is 3, and greatest common multiple is 90
=============================================
沒有留言:
張貼留言