3.1 Greatest common divisor and least common multiple, with global variables and functions

Source: Internet
Author: User
Tags greatest common divisor

Method One: Poor lifting

#include <stdio.h>intMax, Min;intMain () {intMaxintAintb);//can be declared as void because the return value is not required.     intMinintAintb); intA, B; scanf ("%d%d", &a, &b); printf ("\ n"); Max (A, b);//The function can be called directly here, if the global variable assignment will change to 0. Min (A, b);//that is, if you have max = Max (A, A, a, b), you will cause Max to change its value to 0. printf"Max is%d and Min is%d\n", Max, Min); return 0; } intMaxintXinty) {intI, C;  for(i = x > y y:x; i >0; i--){         if(x% i = =0&& y% i = =0) {Max=i;  Break; }     }     return 0;//since it is a global variable, you do not need to return the value of this sentence can be 0 or not write.       } intMinintXintY//I am not using a division, but a poor lift.  {     intI, C;  for(i = x > y x:y; i >0; i++){         if(i% x = =0&& I% y = =0) {Min=i;  Break; }     }     return 0; }

Method Two: Divide

#include <stdio.h>intHCF,LCD;intMain () {voidHcfint,int);//because only the function is borrowed, the global variable is not required to return the value    voidLcdint,int); intu, v; scanf ("%d,%d", &u, &v);    HCF (U,V);    LCD (u,v); printf ("h.c.f =%d\n", HCF);//Remember, two global variables are nested functions, such as the nesting of printfprintf"l.c.d =%d\n", LCD);//Otherwise, the value is wrong    return 0; } voidHcfintUintV//Euclidean Method {     intT, R; if(V >u) {T= u; U = V; v =T; }      while((r = u% v)! =0) {u=v; V=R; } HCF=v;} voidLcdintUintv) {LCD= U * V/HCF;}

From study Counseling

3.1 Greatest common divisor and least common multiple, with global variables and functions

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.