Coding Practice

Write a C program to find HCF (GCD) of two numbers

Find HCF (GCD) of two numbers
Sample Output
Find HCF (GCD) of two numbers.

Enter first number: 153
Enter second number: 81

GCD: 9
Source Code
#include<stdio.h>

int main()
{
    int dividend, divisor, remainder = 1;

    printf("Find HCF (GCD) of two numbers.\n\nEnter first number: ");
    scanf("%d", &dividend);

    printf("Enter second number: ");
    scanf("%d", &divisor);

    while(remainder != 0)
    {
        remainder = dividend % divisor;
        dividend = divisor;
        divisor = remainder;
    }
    printf("\nGCD: %d\n", dividend);

    return 0;
}
Sample Output
Find HCF (GCD) of two numbers.

Enter first number: 30
Enter second number: 12

GCD: 6

No comments:

Post a Comment

Change Theme
X