1.從大到小輸出
內容:寫代碼將三個數按從大到小輸出ide
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int a = 0; int b = 0; int c = 0; scanf("%d%d%d", &a, &b, &c); //a跟b比較 若是a小於b 就把b賦值給a if (a < b) { int max = a; a = b; b = max; } //a跟c比較 若是a小於c 就把c賦值給a if (a < c){ int max = a; a = c; c = max; } //b跟c比較,若是b小於c,就把c賦值給b. if(b<c){ int max = b; b = c; c = max; } printf("%d%d%d,",a, b, c); return 0; }
2.打印3的倍數的數
寫一個代碼打印1-100之間全部3的倍數的數字code
#include <stdio.h> int main() { int i = 3; for (i = 1; i <= 100; i++) { //能被3整除,就是3的倍數 if (i % 3 == 0) { printf("%d,",i); } } return 0; }
觸類旁通
用於自行輸入數字,要求求1-100中這個數的倍數it
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int a = 0; scanf("%d", &a); for (int i = 1; i <= 100; i++) { if (i % a == 0) { printf("%d,",i); } } return 0; }
3.給定兩個數,求這兩個數的最大公約數io
#include <stdio.h> int main() { int a = 76; int b = 16; while (a%b) { int r = a % b; a = b; b = r; } printf("最大公約數爲:%d", b); return 0; }
打印閏年
打印1000年到2000年之間的閏年class
#include <stdio.h> int main(){ int year = 0; int count = 0; //閏年能被4整除,不能被100整除 for (year = 1000; year <= 2000; year++) { if (year % 4 == 0&&year%100!=0) { printf("%d ",year); count++; } //閏年能被400整除 else if (year % 400 == 0) { printf("%d ", year); count++; } } printf("\ncount =%d\n", count); return 0; }
打印素數
打印100-200之間的素數di
#include <stdio.h> int main() { int i = 0; int count = 0; for (i = 100; i <= 200; i++) { //判斷i是否爲素數 //試除法 //只能被1和自己整除 //1.產生2到i-1的數 int j = 0; for (j = 2; j < i; j++) { if (i%j == 0) { break; } } if (j == i) { printf("%d ", i); count++; } } printf("\n 100-200中的素數有%d", count); return 0; }