How to find the greatest common divisor and least common multiple in C language?

coldplay.xixi
Release: 2020-06-30 09:19:52
Original
14513 people have browsed it

How to find the greatest common divisor and least common multiple in C language?

Recommended tutorial: "C Video Tutorial"

How to find the greatest common divisor and minimum in C language common multiple?

The method of finding the greatest common divisor and the least common multiple in C language:

Algorithm for finding the greatest common divisor:

There are two integers a and b:

① a%b gets the remainder c

② If c=0, then b is the greatest common divisor of the two numbers

③ If c≠0, then a =b, b=c, then go back and execute ①

For example, the process of finding the greatest common divisor of 27 and 15 is:

27÷15 remainder 1215÷12 remainder 312÷3 remainder 0. Therefore, 3 is the greatest common divisor

#include int main() /* 辗转相除法求最大公约数 */ { int m, n, a, b, t, c; printf("Input two integer numbers:\n"); scanf("%d%d", &a, &b); m=a; n=b; while(b!=0) /* 余数不为0,继续相除,直到余数为0 */ { c=a%b; a=b; b=c;} printf("The largest common divisor:%d\n", a); printf("The least common multiple:%d\n", m*n/a); }
Copy after login

Find the least common multiple:

#include  int main() { int a,b,A,B; int lol,lpl; printf ("输入两个整数:\n"); scanf ("%d%d",&a,&b); A=a; B=b; if(B) while((A %= B) && (B %= A)); lol = A+B; lpl = a*b/lol; printf ("最小公倍数为:%d\n", lpl); return 0; }
Copy after login

Recommended tutorial: "c#.net Development Graphic Tutorial"

The above is the detailed content of How to find the greatest common divisor and least common multiple in C language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!