gcd_of_n_numbers.cpp File Reference

This program aims at calculating the GCD of n numbers by division method. More...

`#include <iostream>`
Include dependency graph for gcd_of_n_numbers.cpp:

## Functions

int | gcd (int *a, int n)
int | main ()

## Detailed Description

This program aims at calculating the GCD of n numbers by division method.

See also
  gcd_iterative_euclidean.cpp, gcd_recursive_euclidean.cpp

## Function Documentation

## gcd()

int gcd | ( | int * | a,
|  | int | n
| ) |  |

Compute GCD using division algorithm

Parameters

  [in] | a | array of integers to compute GCD for
  [in] | n | number of integers in array `a`

{
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;
}
gcd
int gcd(int *a, int n)
Definition: gcd_of_n_numbers.cpp:15

## main()

int main | ( | void |  | ) |

Main function

{
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;
}
std::cin
std::cout
std::endl
T endl(T... args)
Here is the call graph for this function: