mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
update documentation for gcd_of_n_numbers
This commit is contained in:
parent
894cd9a71d
commit
28ffd29d4c
@ -1,23 +1,42 @@
|
|||||||
// This program aims at calculating the GCD of n numbers by division method
|
/**
|
||||||
|
* @file
|
||||||
|
* @brief This program aims at calculating the GCD of n numbers by division
|
||||||
|
* method
|
||||||
|
*
|
||||||
|
* @see ../math/greatest_common_divisor.cpp
|
||||||
|
* greatest_common_divisor_euclidean.cpp
|
||||||
|
*/
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main() {
|
/** Compute GCD using division algorithm
|
||||||
int n;
|
*
|
||||||
std::cout << "Enter value of n:" << std::endl;
|
* @param[in] a array of integers to compute GCD for
|
||||||
std::cin >> n;
|
* @param[in] n number of integers in array `a`
|
||||||
int *a = new int[n];
|
*/
|
||||||
int i, j, gcd;
|
int gcd(int *a, int n) {
|
||||||
std::cout << "Enter the n numbers:" << std::endl;
|
int j = 1; // to access all elements of the array starting from 1
|
||||||
for (i = 0; i < n; i++) std::cin >> a[i];
|
int gcd = a[0];
|
||||||
j = 1; // to access all elements of the array starting from 1
|
|
||||||
gcd = a[0];
|
|
||||||
while (j < n) {
|
while (j < n) {
|
||||||
if (a[j] % gcd == 0) // value of gcd is as needed so far
|
if (a[j] % gcd == 0) // value of gcd is as needed so far
|
||||||
j++; // so we check for next element
|
j++; // so we check for next element
|
||||||
else
|
else
|
||||||
gcd = a[j] % gcd; // calculating GCD by division method
|
gcd = a[j] % gcd; // calculating GCD by division method
|
||||||
}
|
}
|
||||||
std::cout << "GCD of entered n numbers:" << gcd;
|
return gcd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Main function */
|
||||||
|
int main() {
|
||||||
|
int n;
|
||||||
|
std::cout << "Enter value of n:" << std::endl;
|
||||||
|
std::cin >> n;
|
||||||
|
int *a = new int[n];
|
||||||
|
int i;
|
||||||
|
std::cout << "Enter the n numbers:" << std::endl;
|
||||||
|
for (i = 0; i < n; i++) std::cin >> a[i];
|
||||||
|
|
||||||
|
std::cout << "GCD of entered n numbers:" << gcd(a, n) << std::endl;
|
||||||
|
|
||||||
delete[] a;
|
delete[] a;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user