C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Find HCF (GCD) of two numbers (loop)

Introduction

The HCF (Highest Common Factor) or GCD (Greatest Common Divisor) of two numbers is the largest number that divides both numbers completely without a remainder.

Examples:

  • HCF of 12 and 18 → 6
  • HCF of 20 and 30 → 10

This program calculates the HCF using a for, while and do..while loop.

C Program: Find HCF (GCD) of two numbers (loop)

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int num1, num2, i, hcf = 1;

 

    // Input

    printf("Enter two positive integers: ");

    scanf("%d %d", &num1, &num2);

 

    // Validate input

    if (num1 <= 0 || num2 <= 0) {

        printf("Invalid input! Please enter positive integers.\n");

        return 1;

    }

 

    // Find HCF using for loop

    for (i = 1; i <= num1 && i <= num2; i++) {

        if (num1 % i == 0 && num2 % i == 0) {

            hcf = i;

        }

    }

 

    printf("HCF of %d and %d is: %d\n", num1, num2, hcf);

    return 0;

}

Output

 
OUTPUT 1 :
Enter two positive integers: 12 18
HCF of 12 and 18 is: 6

OUTPUT 2 :
Enter two positive integers: 20 30
HCF of 20 and 30 is: 10

Explanation

  1. User inputs two positive integers num1 and num2.
  2. Initialize hcf = 1.
  3. for loop iterates i from 1 to the smaller of num1 and num2.
  4. If i divides both numbers (num1 % i == 0 && num2 % i == 0), update hcf.
  5. After the loop, hcf stores the highest common factor.

 

C Program: Find HCF (GCD) of two numbers (loop)

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int num1, num2, i = 1, hcf = 1;

 

    // Input

    printf("Enter two positive integers: ");

    scanf("%d %d", &num1, &num2);

 

    // Validate input

    if (num1 <= 0 || num2 <= 0) {

        printf("Invalid input! Please enter positive integers.\n");

        return 1;

    }

 

    // Find HCF using while loop

    while (i <= num1 && i <= num2) {

        if (num1 % i == 0 && num2 % i == 0) {

            hcf = i;

        }

        i++;

    }

 

    printf("HCF of %d and %d is: %d\n", num1, num2, hcf);

    return 0;

}

Output

 
OUTPUT 1 :
Enter two positive integers: 12 18
HCF of 12 and 18 is: 6

OUTPUT 2 :
Enter two positive integers: 20 30
HCF of 20 and 30 is: 10

Explanation

  1. User inputs two positive integers num1 and num2.
  2. Initialize i = 1 and hcf = 1.
  3. while loop iterates i from 1 to the smaller of num1 and num2.
  4. If i divides both numbers (num1 % i == 0 && num2 % i == 0), update hcf.
  5. After the loop, hcf contains the highest common factor.

 

C Program: Find HCF (GCD) of two numbers (loop)

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int num1, num2, i = 1, hcf = 1;

 

    // Input

    printf("Enter two positive integers: ");

    scanf("%d %d", &num1, &num2);

 

    // Validate input

    if (num1 <= 0 || num2 <= 0) {

        printf("Invalid input! Please enter positive integers.\n");

        return 1;

    }

 

    // Find HCF using do...while loop

    if (i <= num1 && i <= num2) {

        do {

            if (num1 % i == 0 && num2 % i == 0) {

                hcf = i;

            }

            i++;

        } while (i <= num1 && i <= num2);

    }

 

    printf("HCF of %d and %d is: %d\n", num1, num2, hcf);

    return 0;

}

Output

 
OUTPUT 1 :
Enter two positive integers: 12 18
HCF of 12 and 18 is: 6

OUTPUT 2 :
Enter two positive integers: 20 30
HCF of 20 and 30 is: 10

Explanation

  1. User inputs two positive integers num1 and num2.
  2. Initialize i = 1 and hcf = 1.
  3. Outer ..while loop iterates from i = 1 to the smaller of num1 and num2.
  4. For each i, check if it divides both numbers. If yes, update hcf.
  5. After the loop, hcf contains the highest common factor.
  6. Ensures that the loop executes at least once even if num1 or num2 is 1.