*******************************************************
GCD of two numbers using non-recursive function
********************************************************
***************
Source Code :
***************
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | Program : To find GCD of two numbers using non-recursive function #include<iostream.h> #include<conio.h> int gcd(int ,int ); void main() { int a,b,d; clrscr(); cout<<"Enter a and b values\n"; cin>>a>>b; d=gcd(a,b); cout<<"GCD of"<<a<<"and"<<b<<"is:"<<" "<<d; getch(); } int gcd(int m,int n) { int d,r; while(n!=0) { d=m%n; if(d==0) r=n; if(d==1) r=1; m=n; n=d; } return r; } /* Output: Enter a and b values 4 24 GCD of 4 and 24 is: 4 */ |
40 total views, 3 views today