UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C program to find GCD of two numbers

C program for how to find GCD of two numbers

by Krishna viswambharan


Posted on 31 Oct 2017 Category: C Views: 26

Edited on 07 Aug 2018


C program to find GCD of two numbers

This program reads two positive integers from the user and finds the GCD using recursion.  This C program compiled successfully and output is also shown.

Source Code:-

#include <stdio.h>
#include <conio.h>
int hcf(int n1, int n2);
int main()
{
   int n1, n2;
   printf("Enter two positive integers: ");
   scanf("%d %d", &n1, &n2);

   printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1,n2));
   getch();
   return 0;
}

int hcf(int n1, int n2)
{
    if (n2 != 0)
       return hcf(n2, n1%n2);
    else 
       return n1;
}

output

 



Leave a Comment:


Advertisement