(一)問題:編寫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;
}
(三)程序運行結果:
,
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!