#include<iostream.h>
int gcf(int i,int j);
void main()
{
int a,b;
cout<<"Please input two nature numbers:"<<endl;
cout<<""<<endl;
cin>>a>>b;
cout<<""<<endl;
if (a>=b)
{
cout<<"The GCF of "<<a<<" and "<<b<<" is "<<gcf(a,b)<<endl;
}
if (a<b)
{
cout<<"The GCF of "<<a<<" and "<<b<<" is "<<gcf(b,a)<<endl;
}
cout<<""<<endl;
}
int gcf(int i,int j)
{
int k;
for(;k!=0;)
{
k=i%j;;
i=j;
j=k;
}
return i;
}
這程式是以輾轉相除法算出兩正整數之最大公因數,之中利用了for loop和呼叫函式,
也算是老師最近教的,希望各位有興趣的同學一起討論..^^Thanx~
--
好無聊
--
※ 發信站: 批踢踢實業坊(ptt.twbbs.org)
◆ From: h178.s87.ts.hin