In this tutorial, we are going to learn a writing program in C to calculate GCD or HCF. Basically, Both GCD and HCF are the same. GCD full form is Greatest common divisor and HCF means Highest Common Divisor.

Before starting the writing a program in C for HCF or GCD let’s understand HCF/GCD first.

## What is GCD/HCF? How to calculate it?

GCD stands for **greatest common divisor** (**GCD**), It is a largest non-zero positive integer of two or more integers that divides each of the integers.

GCD is same as HCF. It can be justify like “greatest common divisor”, the adjective “greatest” may be replaced by “highest”, and the word “divisor” may be replaced by “factor”, so that other name of GCD can be HCF.

**How to calculate GCD/HCF?**

To calculate GCD of any two or more number, first we will find the factors of the numbers. After that will select the common number and multipy them, resultant will be GCD.

**Let’s see an example**

Suppose we need to calculate the **GCD of 26 and 38**.

**Factors of 26** = 2 * 13

**Factors of 38** = 2 * 19

Here 2 is only the common. So** GCD(26,38) will be 2** because only 2 is the highest number that can divide both 26 and 38.

## C Program to calculate GCD of two numbers

#include <stdio.h> int main() { int num1, num2, i, gcd; printf("Program to find HCF or GCD of two numbers\n"); printf("Enter the first number: "); scanf("%d", &num1); printf("Enter the second number: "); scanf("%d", &num2); for(i=1; i <= num1 && i <= num2; ++i) { if(num1%i==0 && num2%i==0) gcd = i; } printf("G.C.D of number %d and %d is %d", num1, num2, gcd); return 0; }

**Output**

#### Share on

## Also Prepare Below Important Question

- Java Program to Perform Left Rotation on Array Elements by Two
- Java Program to Perform Right Rotation on Array Elements by Two
- Java Program to Print Odd Numbers from Array
- Java Program to Print All Even Numbers in Array
- Java Program to Find the Sum of Array Elements
- Java Program to Delete Element of Array At Given Location
- Java Program to Delete a given Element of Array
- Java Program to Delete Element at End of Array
- Java Program to Insert Element in Array at given Location
- Java Program to Insert Element At the End of Array
- Java Program to Print Length of an Array
- Reverse Array without using Second Array or inplace Reversal Java Program
- Java Program to Print Array in Reverse Order
- Java Program to Sort String Character in Descending order
- Java Program to Sort String in Ascending Order
- Java Program to Print non Repeating Characters in String
- Java Program to Find Sum of Integers in the String
- Java Program to Remove Duplicates From String
- Java Program to Concatenate two Strings
- Java Program to Check if two Strings are Same

## Interview Questions Categories

### C Programming Interview Preparation

### Core Java Programming Interview Preparation

- Core Java Programming Coding Questions
- Core Java Pattern Programming Questions
- Core Java Programming Interview Questions