The need:
This program was given to me as a lab assignment.
The code:
---------------------------------------
//Finding HCF
#include<stdio.h>
main()
{
printf("Enter two integers to get their hcf ..\n");
long int i,j,k;
scanf("%ld %ld",&i,&j);
ranu:
k=i%j;
if(k!=0)
{i=j;
j=k;
goto ranu;}
else
printf("The hcf of given numbers is %ld\n\n\n",j);
}
---------------------------------------
#include<stdio.h>
main()
{
printf("Enter two integers to get their hcf ..\n");
long int i,j,k;
scanf("%ld %ld",&i,&j);
ranu:
k=i%j;
if(k!=0)
{i=j;
j=k;
goto ranu;}
else
printf("The hcf of given numbers is %ld\n\n\n",j);
}
---------------------------------------
The approach:
In this program I have used the same division method for finding GCD that you use when you calculate GCD with a paper pen(If you have ever used). In the same way in each run of loop I am making remainder the divisor and divisor the dividend if the remainder is not 0. If the remainder is found 0 then loop terminates and corresponding value of divisor is the GCD.
In this program I used 'goto' because by that time I was not aware of 'for' loop. I think you are smart enough to rewrite this program using 'for' and 'while' loop if you have gone through program12, program13 and program14 of this blog so I am not going to put those programs in this blog.
One of the best programming i ever seen, simple & blast.
ReplyDelete