Write a program in C language, ask to input two positive integers, find their maximum common divisor and output
时间: 2024-05-08 07:20:14 浏览: 86
the result.
好的,您可以输入以下代码来实现您的需求:
```
#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);
int result = gcd(a, b);
printf("它们的最大公约数是:%d", result);
return 0;
}
```
这个程序可以在 C 语言中运行,它会要求用户输入两个正整数,然后通过最大公约数的算法来计算它们的最大公约数并输出结果。
相关问题
1. Dividing Two Integers (2’) Given two positive integers A and B (A is a divisor and B is a dividend, as dividend and divisor. Output the integer result of A / B. Please compute the result without using multiplication (*), division (/) and mod(%) operator. Note: 1. The integer division should truncate toward zero, which means losing its fractional part. 2. Divisor cannot be the zero and the output must be a number.
To solve this problem, we can use repeated subtraction to find how many times the divisor can be subtracted from the dividend without going below zero. This will give us the integer result of the division. Here's the code in Python:
```
def divide(divisor, dividend):
if divisor == 0:
return None # Divisor cannot be zero
if dividend == 0:
return 0 # Dividend is zero, so result is zero
# determine sign of the result
negative = (divisor < 0) ^ (dividend < 0)
# convert to positive numbers
divisor = abs(divisor)
dividend = abs(dividend)
quotient = 0
while dividend >= divisor:
dividend -= divisor
quotient += 1
# restore sign of the result
if negative:
quotient = -quotient
return quotient
```
In this code, we first check if the divisor is zero, which is not allowed. If the dividend is zero, then the result is zero. We also determine the sign of the result by checking if the divisor and dividend have different signs.
We then convert both the divisor and dividend to positive numbers to simplify the repeated subtraction. We use a while loop to subtract the divisor from the dividend until we can no longer do so without going below zero. We keep track of how many times we subtracted the divisor, which is the integer result of the division.
Finally, we restore the sign of the result if necessary and return it. This code should work for any positive integers A and B, as long as A is not zero.
Write a program to find all numbers between 'a' and 'b' (both included) such that each digit of the number is an even number, and print the results. Input There are two non-negative integers in one line: a and b Output Directly print the list
Sure, here's a Python program that will solve the problem:
```python
a, b = map(int, input().split())
# Define a function to check if a number has all even digits
def all_even_digits(n):
while n > 0:
if (n % 10) % 2 != 0:
return False
n //= 10
return True
# Loop through all numbers between a and b, check if they have all even digits, and print them if they do
for i in range(a, b+1):
if all_even_digits(i):
print(i)
```
To use this program, simply input the values of 'a' and 'b' on the first line, separated by a space. The program will then output a list of all numbers between 'a' and 'b' (inclusive) that have all even digits.
阅读全文