C-language implementation of the 3-digit LCM and GCD

Source: Internet
Author: User

The GCD and LCM of 3 numbers are obtained according to the method of LCM and GCD of two numbers.
The main idea is to use a%b==0&a%c==0 to judge whether or not to satisfy, and then use the cycle count.

#include <stdio.h>
int main ()
{
    int x,y,z,tem,i=1,num;
    int k=1;
    printf ("Enter 3: \ n");
    scanf ("%d%d%d", &x,&y,&z);
    tem=x;       Suppose x minimum
    if (tem>y)     //looks for a minimum value of three numbers
    {
        tem=y;
    }
    if (tem>z)
    {
        tem=z;
    }

    for (i=1;i<=tem;i++)   //Set Number of loops
    {
        if (x%i==0&&y%i==0&&z%i==0)  // GCD conditions to meet
        {
            num=i
        }
    }
    while (1)///Forever Set, unconditional loop
    {
        if (k%x==0&k%y==0&k%z==0)  ///judge whether the LCM break
         ;
         else
         k++;
    }
    printf ("Two number of GCD:%d\n", num);
    printf ("Two-digit LCM:%d\n", k);
}

Results test:

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.