GCD and LCM Examples of two digits in C + +

Source: Internet
Author: User
Tags gcd

1, to find the number of GCD two:

#include <iostream.h>
int maxye(int a,int b)
{
int temp;
while(a%b)
{
  temp=b;
  b=a%b;
  a=temp;
}
return b;
}

void main()
{
int aa,bb;
cout<<"请输入第一个数:";
cin>>aa;
cout<<"\n请输入第二个数:";
cin>>bb;
cout<<"这二个数的最大公约数是:"<<maxye(aa,bb) <<endl;
}

2, to find the number of LCM two

 #include <iostream.h>
int maxye(int a,int b)
{
int temp;
if(a<b)
{
  temp=a;
  a=b;
  b=temp;
}
for(int i=1;i<=b;i++)
{
if(!((a*i)%b))
{
  return a*i;
}
}

}

void main()
{
int aa,bb;
cout<<"请输入第一个数:";
cin>>aa;
cout<<"\n请输入第二个数:";
cin>>bb;
cout<<"这二个数的最小公倍数是:"<<maxye(aa,bb) <<endl;
}

Comments:

GCD: The largest of a number of integers in a common convention;

For example: in 2, 4, 6, 2 is 2,4,6 's gcd.

LCM, for two integers, refers to the smallest of the two numbers in a common multiple.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.