(a). Without using Recursive function.
#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void gcd (int, int); //func. declaration.
void main( )
{
int a, b;
clrscr( );
printf("Enter the two integer values:");
scanf("%d%d", &a, &b);
printf("\nGiven numbers are: %d and %d", a, b);
gcd(a, b); // calling function.
getch( );
}
void gcd( int x, int y) //called function.
{
int z;
for( ; ; ) //This is empty for loop.
{
z= x%y;
if( z==0) break;
x=y;
y=z;
}
printf("\nThe GCD is: %d", y);
}
Note:- Take the values as 23 and 3 , then GCd is 3.
This logic works, when a>b. (ie., 23>3 )
I'm not mentioned the logic when b>a. ( for that, we need if-else statement)
Explanation:-
Here i'm taking empty for loop becoz i dont have initial value , condition and increment or decrement.
This for loop will ends, when 'z' becomes zero. ie.,(z==0)
No comments:
Post a Comment