a). Without using Recursive function.
void gcd (int, int); //func. declaration.
void main( )
{
int a, b;
clrscr( );
cout<<"Enter the two integer values:";
cin>>a >>b;
cout<<"Given numbers are:" << a <<"and" << 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;
}
cout<<"The GCD is:" << 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)
Wednesday, February 25, 2009
Subscribe to:
Post Comments (Atom)
SIR,
ReplyDeleteI WAS ENABLE TO GET THIS LOGIC. PLEASE KINDLY ONCE EXPLAIN THE ABOVE PROGRAM CODE IN DETAIL