C Language Beginner (4)

Source: Internet
Author: User
Tags gcd greatest common divisor

Title: Greatest common divisor of A and B

Analysis: First we need to know what greatest common divisor is, which means that two or more integers have the largest number in total. Well, know what greatest common divisor is, you can solve it, then it is equivalent to compare the number of the approximate, take its equal to the largest one on the right, right? What is the approximate??? An approximate number is one that divides a or b evenly.

All right, let's take a look at the code.

Code:

1#include <stdio.h>2 voidgcdLong intALong intb//Sub-function seeking GCD3 {4     Long inti,t=0, j=0, k=0, max,q=0;5     Long intx[ the],y[ the];//x[] A divisor that holds the first number, y[] holds the number of the second number divisible by the first divisor6      for(i=a;i>0; i--)//x[]7     {8         if(a%i==0)9x[j++]=i;Tent++; One     } A      for(i=0; i<t;i++)//y[] -     { -         if(b%x[i]==0) they[k++]=X[i]; -q++; -     } -max=y[0];//Select the largest one +      for(i=0; i<q;i++) -     { +         if(max<Y[i]) A         { atmax=Y[i]; -         } -     } -printf"gcd=%ld\n", Max);//Output - } - voidMain () in { -     Long intb; to     Long intx[ -],y[ -]; +      while(1) -     { theprintf"Please enter the number between 0 to 255 \ n");//Enter both numbers *scanf"%ld%ld",&a,&b); $ gcd (A, b);Panax Notoginseng     } -}

Let's take a look at a few answers:

Isn't it easy?

Welcome to contact My qq:2516985331

Learn together ~

C Language Beginner (4)

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.