iT邦幫忙

2023 iThome 鐵人賽

0
自我挑戰組

C語言精讀研習系列 第 46

使用for迴圈與變數遞減當條件來求出gcd與lcm

  • 分享至 

  • xImage
  •  

完整程式碼

#include <stdio.h>

int main(void){
	int A, B, i, gcd;
	scanf("%d %d", &A, &B);
	for(i=A; i>=1; i--){
		if(A%i == 0 && B%i ==0){
			gcd = i;
			break;
		}
	}
	printf("%d\n", gcd);
	printf("%d", A*B/gcd);
	
	return 0;
}

輸出結果
https://ithelp.ithome.com.tw/upload/images/20240328/20160744EnNHOJcXjY.png

參考資料:TQC+ C第2版


上一篇
輸出格式的「參數」控制輸出的「對齊方式」與「長度」
系列文
C語言精讀研習46
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言