C language: find the greatest common divisor and greatest common multiple of two numbers

Simple c
find the greatest common divisor of two numbers.
(1) Euclid's algorithm
Insert picture description here

Insert picture description here
Insert picture description hereI tried it many times at the beginning, but no results appeared when I entered. I searched for many times and failed to find the error. Finally, I found that the variable assignment in scanf did not add &. Next blog will talk about what & is.
(2) Enumeration method
First find the smaller number of m and n, such as n. Then m and n are divided by n at the same time, and then n– in turn; until the remainder of m and n divided by the change of n is 0, then the change of n is the greatest common divisor.
(3) More phase
reduction method First judge whether they are all even numbers, and if so, use 2 to reduce . Then use the larger number to subtract the smaller number, and then compare the difference with the smaller number, and decrease the number with the larger number in sequence until the subtraction and the difference are equal. Insert picture description here
Insert picture description hereThis program confused me for a long time at first. At first, I thought that the if nested for statement was wrong. Then I realized that the power of the C language cannot be expressed by 2^n, which leads to the result that is always wrong, but the power can be. It is represented by pow (x, y) in the math.h library.
The greatest common multiple is
because the greatest common multiple is equal to the product of two numbers divided by their greatest common factor.

Guess you like

Origin blog.csdn.net/m0_52405419/article/details/115361298