int getGCD(int a, int b) { if(b==0) return a; else return getGCD(b, a%b); }
int main(void){ char s[SIZE]; // hold the string that user inputs. int c; int x; // x value to be input int y; // y value to be input int i = 0; // for loop count // prompt user input data puts("Please enter two integer, and we can get GCD"); printf("-------------------------------------------\n"); // user getchar to read each character while(i < SIZE - 1 && (c = getchar()) != '\n'){ s[i++] = c; } s[i] = '\0'; // end the string sscanf(s, "%d%d", &x,&y); // Convert string into number int gcd = getGCD(x,y); // Invoke function to get gcd printf("--------------------------------------------\n"); if (gcd == 0){ printf("Your input is wrong, GCD is based on non-negative number and not zero.\n"); } else { printf("We can get GCD is %d for %d and %d\n", gcd, x, y); } system("pause"); return 0; }
//get the least common multiple int getLCM(int *array, int size){ int x; int y; int temp; int gcd = 1; int result = 1; int i; x = array[0]; for (i = 0; i < size - 1; ++i) { y = array[i+1]; if (x < y) { temp = x; x = y; y = temp; } gcd = getGCD(x, y); printf("the gcd is: %d\n", gcd); result = (x * y) / gcd; x = result; } return result; }
void Q3() { int gcd, num, denom; printf("Enter a fraction: "); scanf("%d/%d", &num, &denom); gcd = getGCD(num, denom); printf("In lowest terms: %d/%d\n", num / gcd, denom / gcd); }
int getGCD(int x, int y){ if (x % y == 0) { return y; }else{ return getGCD(y, x % y); } }
void Menu::executeGCD() { int nFirst = 0, nSecond = 0; cout << "\t최대공약수 구하기 함수를 선택하셨습니다." << endl; cout << "■ 두 개의 숫자를 입력해주세요." << endl; cout << "- 첫 번째 수 : "; cin >> nFirst; cout << "- 두 번째 수 : "; cin >> nSecond; int nGcd = getGCD(nFirst, nSecond); cout << "▶ " << nFirst << "와 " << nSecond << "의 최대공약수는 " << nGcd << " 입니다." << endl; }
int getLCM(int a, int b) { return (a*b)/getGCD(a, b); }