c语言编写一个程序用碾转相除法求两个整数的最大公约数
时间: 2024-06-08 19:11:03 浏览: 11
下面是用 C 语言编写的碾转相除法求两个整数的最大公约数的程序:
```c
#include <stdio.h>
int main() {
int a, b, temp;
printf("请输入两个整数:\n");
scanf("%d %d", &a, &b);
while (b != 0) {
temp = a % b;
a = b;
b = temp;
}
printf("最大公约数为:%d\n", a);
return 0;
}
```
程序首先要求用户输入两个整数,然后进入一个 while 循环,循环条件是 b 不等于 0。在循环内部,用 temp 变量存储 a 除以 b 的余数,然后将 b 赋值给 a,将 temp 赋值给 b,继续循环。当 b 等于 0 时,循环结束,此时 a 的值就是最大公约数,将其输出即可。
相关问题
用c语言编写程序,用辗转相除法求两个非负整数的最大公约数
好的,这个问题很简单。以下是用 C 语言编写的代码示例:
```
#include <stdio.h>
int gcd(int a, int b)
{
int temp;
while (b != 0)
{
temp = b;
b = a % b;
a = temp;
}
return a;
}
int main()
{
int n1, n2, result;
printf("请输入两个非负整数:\n");
scanf("%d %d", &n1, &n2);
result = gcd(n1, n2);
printf("最大公约数是 %d\n", result);
return 0;
}
```
这是一个使用辗转相除法来求两个非负整数最大公约数的示例程序。
用c语言编写程序用辗转相除法求两个整数的最大公约数。
好的,这个问题属于计算机科学领域。下面是使用辗转相除法求两个整数的最大公约数的C语言程序:
```
#include <stdio.h>
int gcd(int a, int b) {
int temp;
while (b != 0) {
temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int a, b;
printf("请输入两个整数:");
scanf("%d %d", &a, &b);
printf("它们的最大公约数为:%d\n", gcd(a, b));
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)