Two nonnegative integers for greatest common divisor (Euclidean algorithm)---C + +

Source: Internet
Author: User
Tags greatest common divisor

#include <iostream>using namespacestd;//Euclidean algorithm for greatest common divisor of two nonnegative integersintGetdivisor (intAintb) {    intmax,min; Max=A; Min=b; //two number of large number modulus decimal, if the result is not 0, then discard the large number, the decimal and the result of the modulo operation to size, continue to take the modulo operation//recursive solution in turn until the modulo operation result is 0, then the decimal is greatest common divisor    if(max%min!=0){            if(max%min>min)returnGetdivisor (max%min,min); Else            returnGetdivisor (min,max%min); }Else        returnmin;}intMain () {//freopen ("D:\\algorithm\\testdata.txt", "R", stdin);    inta,b,max,min; intresult; cout<<"Please enter two non-negative integers:" ; CIN>> a >>b; A>b? (max = A,min = b):(max = B,min =a); Result=Getdivisor (A, b); cout<< a <<"with the"<< b <<"the greatest common divisor for"<< result<<Endl; return 0; }

Two nonnegative integers for greatest common divisor (Euclidean algorithm)---C + +

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.