Gcd of N Numbers

/**
 * @file
 * @brief This program aims at calculating the GCD of n numbers by division
 * method
 *
 * @see gcd_iterative_euclidean.cpp, gcd_recursive_euclidean.cpp
 */
#include <iostream>

/** Compute GCD using division algorithm
 *
 * @param[in] a array of integers to compute GCD for
 * @param[in] n number of integers in array `a`
 */
int gcd(int *a, int n) {
    int j = 1;  // to access all elements of the array starting from 1
    int gcd = a[0];
    while (j < n) {
        if (a[j] % gcd == 0)  // value of gcd is as needed so far
            j++;              // so we check for next element
        else
            gcd = a[j] % gcd;  // calculating GCD by division method
    }
    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;
    return 0;
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.