C program to find the GCD of two numbers - My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Sunday, January 5, 2020

C program to find the GCD of two numbers - My CS Tutorial

C program to find the GCD (GREATEST COMMON DIVISIOR) of two numbers.

C program to find HCF ( HIGHEST COMMON FACTOR) of two numbers.

In this tutorial we will learn how to write a c program that find the GCD of two numbers.GCD or HCF of two numbers is the greatest number which can be divided by both numbers.


C program code


#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,i,gcd;
clrscr();
printf("\n ENTER THE VALUE OF N1(FIRST NUMBER):\n ");
scanf("%d",&n1);
printf("\n ENTER THE VALUE OF N2(SECOND NUMBER):\n ");
scanf("%d",&n2);
for(i=1;i<=n1&&i<=n2;i++)
{
if(n1%i==0&&n2%i==0)
gcd=i;
}
printf("\n THE GCD IS:\n %d",gcd);
getch();
}

OUTPUT:





In this output we will see, this program find the GCD of two numbers.
In the first we will enter two numbers and stored in variables n1 and n2.
The GCD stored in gcd variable.


Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment