2016年4月28日 星期四

使用C語言,判斷是否為質數

程式碼:

#include <stdio.h>

main()

{

int n, c = 2;

printf("請輸入一個整數以判斷是否為質數\n");

scanf("%d",&n);

for ( c = 2 ; c <= n - 1 ; c++ )

{

if ( n%c == 0 )

{

printf("%d 不是質數\n", n);

break;

}

}

if ( c == n )

printf("%d 是質數\n", n);

return 0;

}

執行結果:

請輸入一個整數以判斷是否為質數

29

29 是質數

--------------------------------

Process exited after 16.72 seconds with return value 0

請按任意鍵繼續 . . .

p.s.

執行C語言工具

Windows系統 : 下載Dev C++

Android系統 : 下載CppDroid

2016年4月25日 星期一

使用C語言,幫您找出質數。

程式碼:

‪#‎include‬ < stdio.h >

int main()

{

int n, i = 3, count, c;

printf("請問從2開始由小到大,您要幾個質數?\n");

scanf("%d",&n);

if ( n >= 1 )

{

printf("前 %d 個質數是 :\n",n);

printf("2\n");

}

for ( count = 2 ; count <= n ; )

{

for ( c = 2 ; c <= i - 1 ; c++ )

{

if ( i%c == 0 )

break;

}

if ( c == i )

{

printf("%d\n",i);

count++;

}

i++;

}

return 0;

}

執行結果:

請問從2開始由小到大,您要幾個質數?

10

前 10 個質數是 :

2

3

5

7

11

13

17

19

23

29

--------------------------------

Process exited after 5.489 seconds with return value 0

請按任意鍵繼續 . . .

p.s.

執行C語言工具

Windows系統 : 下載Dev C++

Android系統 : 下載CppDroid

2016年1月14日 星期四

在Android手機,平板玩C語言

/*在Android手機,平板玩C語言,

請下載安裝Cppdroid,即可編譯執行C語言,

1.長按此區然後複製

2.將此程式貼在Cppdroid程式區

3.按儲存,編譯,執行即可*/

/*列印出 Hello, World! 的C語言*/

‪#‎include ‬<stdio.h>

int main()

{

printf("Hello, World! \n");

return 0;

}

/*Android系統C語言的免費代碼編譯器介紹

http://seelab.zjsu.edu.cn:4000/node/4649

*/

/*斜線和星號內的文字會被忽略,不會影響程式運作*/

2015年12月30日 星期三

國小第一堂數學課

各位小朋友大家好!今天你們國小的第一堂數學課上的是計算a+b=?

請大家輸入下列程式,然後計算兩個數的合。

#‎include ‬<stdio.h>

int main()

{

int a, b, c;

printf("請輸入兩個整數,然後它會幫你相加。\n");

scanf("%d%d",&a,&b);

c = a + b;

printf("相加的答案 = %d\n",c);

return 0;

}

p.s.各位小朋友,我們的老祖宗曾經用手指算數學,也用過算盤,還有那個小小的電子計算機算的,甚至用什麼Apple II 算的,這些都落伍了!我們今天是用最快速的 C 語言來計算,在這變動的世界環境裡,隨時改變程式設計,才可以適應不同的需求。

查查看2016是否為閏年(leap year)?

‪#include <stdio.h>

int main()

{

int year;

printf("Enter a year to check if it is a leap year\n");

scanf("%d", &year);

if ( year%400 == 0)

printf("%d is a leap year.\n", year);

else if ( year%100 == 0)

printf("%d is not a leap year.\n", year);

else if ( year%4 == 0 )

printf("%d is a leap year.\n", year);

else

printf("%d is not a leap year.\n", year);

return 0;

}

p.s. 在Windows系統玩C語言

2015年8月4日 星期二

用 || (或) 來判斷英文字母是否為母音

#include <stdio.h>

int main()

{

char ch;

printf("Enter a character\n");

scanf("%c", &ch);

if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U') printf("%c is a vowel.\n", ch);

else

printf("%c is not a vowel.\n", ch);

return 0;

}

p.s.每次執行程式的時候,只能判斷一次,不能重複判斷

首頁

2015年7月19日 星期日

列印字串 Hello World! 的解說

#‎include ‬<stdio.h> //#include:引入stdio.h檔案     stdio.h:系統的引入檔

int main() //int : 宣告其回傳值為整數    main : 函數(本程式的主要函數)

{

printf("Hello, World! \n"); //printf:輸出到螢幕,列印出Hello world!   \n : 換行字元

return 0; //將整數0回傳給呼叫main的函數

}

首頁