定义函数Gcd计算两个整数的最大公约数,如果输入的数不是正整数,则返回-1。然后再在主函数中调用该函数,数据的输入输出在主函数中完成。
**主函数中输入提示信息格式要求:"Input a,b:"
主函数中输入格式:"%d,%d"
**主函数中输出提示信息要求:
如果输入的数不是正整数,则输出"Input number should be positive!\n",否则按如下格式输出"Greatest Common Divisor of %d and %d is %d\n"
程序运行结果示例:
Input a,b:58,44
Greatest Common Divisor of 58 and 44 is 2
Input a,b:-16,24
Input number should be positive!
#include<stdio.h>
int Gcd(int, int);
int main()
{
int a, b, i = 0;
printf("Input a,b:");
do {
if (i != 0)printf("Input number should be positive!\n");
scanf_s("%d,%d", &a, &b);
i++;
} while (Gcd(a, b) == -1);
printf("Greatest Common Divisor of %d and %d is %d\n", a, b, Gcd(a, b));
}
int Gcd(int a, int b)
{
int c=b;
if (a < 1 || b < 1)return -1;
do
{
c = a % b;
a = b;
b = c;
c = a % b;
} while (c);
return b;
}
定义函数Gcd计算两个整数的最大公约数,如果输入的数不是正整数,则返回-1。然后再在主函数中调用该函数,数据的输入输出在主函数中完成。 **主函数中输入提示信息格式要求:"Input a,b:" 主函数中输入格式:"%d,%d" **主函数中输出提示信息要求: 如果输入的数不是正整数,则输出"Input number should be positive!\n",否则按如下格式输出"Greatest Common Divisor of %d and %d is .