- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
GCD of an array of numbers in java
Program
Following is the example to calculate the GCD of the numbers of an array.
public class GCDOfArrayofNumbers{ public static int gcd(int a,int b){ int res = 0; while (b > 0){ int temp = b; b = a % b; a = temp; res = a; } return res; } public static void main(String arg[]){ int[] myArray = {3, 6, 8}; int result = gcd(myArray[0],myArray[1]); for(int i = 2; i < myArray.length; i++){ result = gcd(result, myArray[i]); } System.out.println("Gcd of n numbers is: "+result); } }
Output
GCD of n numbers is: 1
- Related Articles
- Java Program for GCD of more than two (or array) numbers
- GCD and LCM of two numbers in Java
- LCM of an array of numbers in Java
- Java Program to Find GCD of two Numbers
- GCD of more than two (or array) numbers in Python Program
- Removal of negative numbers from an array in Java
- Python Program for GCD of more than two (or array) numbers
- C++ Program for GCD of more than two (or array) numbers?
- Find GCD of two numbers
- Java program to find the GCD or HCF of two numbers
- C++ Program for GCD 0.of more than two (or array) numbers?
- Finding LCM of more than two (or array) numbers without using GCD in C++
- Find pair with maximum GCD in an array in C++
- Find GCD of factorial of elements of given array in C++
- Swift Program to Find GCD of two Numbers

Advertisements