Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
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]aarray of integers to compute GCD for
[in]nnumber of integers in array a
15 {
16 int j = 1; // to access all elements of the array starting from 1
17 int gcd = a[0];
18 while (j < n) {
19 if (a[j] % gcd == 0) // value of gcd is as needed so far
20 j++; // so we check for next element
21 else
22 gcd = a[j] % gcd; // calculating GCD by division method
23 }
24 return gcd;
25}
int gcd(int *a, int n)
Definition gcd_of_n_numbers.cpp:15
Here is the call graph for this function:

◆ main()

int main ( void )

Main function

28 {
29 int n;
30 std::cout << "Enter value of n:" << std::endl;
31 std::cin >> n;
32 int *a = new int[n];
33 int i;
34 std::cout << "Enter the n numbers:" << std::endl;
35 for (i = 0; i < n; i++) std::cin >> a[i];
36
37 std::cout << "GCD of entered n numbers:" << gcd(a, n) << std::endl;
38
39 delete[] a;
40 return 0;
41}
T endl(T... args)
Here is the call graph for this function: