1 #include<stdio.h> 2 #include<stdlib.h> 3 //最大公约数 4 int main() 5 { 6 int num1, num2; 7 printf("input two numbers:\n"); 8 scanf("%d %d",&num1,&num2); 9 10 int min;//记录两个数中较小的数 11 12 if(num1 <= 0 || num2 <= 0) 13 { 14 printf("Error!\n"); 15 return -1; 16 } 17 18 if(num1 > num2) 19 min = num2; 20 else 21 min = num1; 22 23 while(min != 0) 24 { 25 if(num1 % min == 0 && num2 % min == 0) 26 break; 27 min--; 28 } 29 30 system("pause"); 31 return 0; 32 } 33 //最小公倍数 34 int lcm(int num1, int num2) 35 { 36 int max;//记录两个数中较大的数 37 if(num1 <= 0 || num2 <= 0) 38 { 39 printf("Error!\n"); 40 return -1; 41 } 42 if(num1 > num2) 43 max = num1; 44 else 45 max = num1; 46 while(max != 0) 47 { 48 if(num1 % max == 0 && num2 % max == 0) 49 return max; 50 max++; 51 } 52 return -1; 53 }
原文:http://www.cnblogs.com/cpsmile/p/4776541.html