「C言語」自然数の最小公倍数を求めるサンプル
使用例
#include <stdio.h>
int main(void)
{
int x, y, result, kk, temp;
printf("自然数を入力\n");
printf("自然数1: ");
scanf("%d", &x);
printf("自然数2: ");
scanf("%d", &y);
kk = x * y;
/* 自然数 */
if(x < y){
temp =x;
x = y;
y = temp;
}
result = x % y;
while(result!=0){
x = y;
y = result;
result = x % y;
}
/* 最小公倍数 */
printf("最小公倍数: %d\n", kk/y);
return 0;
}
#include <stdio.h>
int main(void)
{
int x, y, result, kk, temp;
printf("自然数を入力\n");
printf("自然数1: ");
scanf("%d", &x);
printf("自然数2: ");
scanf("%d", &y);
kk = x * y;
/* 自然数 */
if(x < y){
temp =x;
x = y;
y = temp;
}
result = x % y;
while(result!=0){
x = y;
y = result;
result = x % y;
}
/* 最小公倍数 */
printf("最小公倍数: %d\n", kk/y);
return 0;
}
#include <stdio.h> int main(void) { int x, y, result, kk, temp; printf("自然数を入力\n"); printf("自然数1: "); scanf("%d", &x); printf("自然数2: "); scanf("%d", &y); kk = x * y; /* 自然数 */ if(x < y){ temp =x; x = y; y = temp; } result = x % y; while(result!=0){ x = y; y = result; result = x % y; } /* 最小公倍数 */ printf("最小公倍数: %d\n", kk/y); return 0; }
実行結果
>gcc 100.c
>a.exe
自然数を入力
自然数1: 5
自然数2: 12
最小公倍数: 60