(一)问题:编写C 程序,求出任意两个整数的最小公倍数和最大公约数。
(二)程序代码:
#include<iostream>
#include<windows.h>
using namespace std;
int f1(int a,int b)//定义求两个整数最小公倍数的函数
{
int n=0;
for(int i=1;i<=b;i )
if((a*i)%b==0)
{
n=a*i;
break;
}
return n;
}
int f2(int x,int y)//定义求两个整数最大公约数的函数
{
return (x*y)/f1(x,y);
}
int main()
{
int n1,n2;
cout<<"Enter n1,n2:";
cin>>n1>>n2;
cout<<n1<<ends<<n2;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN FOREGROUND_INTENSITY);
cout<<"最小公倍数为:";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED FOREGROUND_GREEN FOREGROUND_BLUE FOREGROUND_INTENSITY);
cout<<f1(n1,n2)<<endl;
cout<<n1<<ends<<n2;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN FOREGROUND_INTENSITY);
cout<<"最大公约数为:";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED FOREGROUND_GREEN FOREGROUND_BLUE FOREGROUND_INTENSITY);
cout<<f2(n1,n2)<<endl;
system("pause>nul");
return 0;
}
(三)程序运行结果:
,