0 like 0 dislike
10.1k views

質數(Prime number),又称素数,指在大於1的自然数中,除了1和該数自身外,無法被其他自然数整除的数(也可定義為只有1與該數本身两个因数的数)。 大於1的自然數若不是質數,則稱之為合數。 例如,5是個質數,因為其正因數只有1與5,而6則是個合數,因為除了1與6外,2與3也是其正因數。

 

請寫一個程式,讓使用者輸入一個正整數,程式會判斷這個正整數是不是質數。

 

輸入範例1:

9

輸出範例1:

not a prime

輸入範例2:

2

輸出範例2:

is a prime

[Exercise] Coding (C) - asked in 作業 by (18k points)
ID: 13581 - Available when: Unlimited - Due to: Unlimited
1 flag | 10.1k views
0 0
Called for Help
0 0
Called for Help
0 0
Called for Help

117 Answers

0 like 0 dislike
Hidden content!
#include<stdio.h>

#include<stdlib.h>



int main()

{

    
*** * *** * * ***** ** x=0,y=0;

    
* * **** * ** ** * * ** * *

    
* * **** ** * * * ** * ** *** * * **

    

    {
* *** * ** * ** **** * ** ** ** ****** * *** * *
*** *** *** * ** **** ******* * ** * ** * *** *
* * ** * ** ** * * * ** **** ** * *
*** ** * ** ** * ****** * ** * * ** * ********** *** * * *** * * * * * * * ** *** * *** * * *** ** * ** * a prime");
* **** * ****** ** ****** ** ** ***** ***** ** ** ****** ***** ** * *** * **** *********** * * *
**** ** ** ** * **** *** * ** *** ** ** ***** ** * *** ** * *** * * **** * * ** ***** *** * ** ** ****** * * *** *** * * **
* *** * *** * * * *** * * * * * *** * * *** ** *** ** *** * * **** ******* * * * * * ** ** * **
* **** *** * *** * *** *** *** ******* *** ** * ************ ** * * *** ** *** ** ***** * * ** 0;
* ** * *** * * * * ** *** * **** * * * *** * ***** * *** ** *** * *** ** **** * ******** *** *
** * ** * * * * * * ** * * * ** ** *** ***
* * * ** ***** **** * * ** *** * * * ***
** ** ** ** * * ** ** ** ** * ** *** ** ** ***** ** * *** * **

    }

    
* * * ** ***** * ** ** a prime");

    
* ** **** * * * * ***** ** * ***
** *** ** *** * ** * * * 0;

    

}
answered by (102 points)
0 like 0 dislike
Hidden content!
***** **** ** **

* * **

{

n;

int **

** * **** ******* * **

*** ***** **

** * *** * ** ***

{

if ** *

{

**

* *

}

}

if ***

* * * ** ** ** * **** * ** a **** ** ** ***

**

** *** *** ** * ** * ** * a ** ** * ** *** ** *

}
answered by (-98 points)
0 like 0 dislike
Hidden content!
* * ** ** *** * ****

*** *

{

int n;

int *

* * * * * ** *****

***** * **

for * * * * * *** * *

{

if ***

{

* *

* *

}

}

if **

* *** * *** * * * * * * * * * *

*

** * ** ** * *** *** ** ** * * *** * * **

}
answered by (-98 points)
0 like 0 dislike
Hidden content!
#include *** ** * ****

#include * * * ** * **



int main()

{

int i,n;




**** ** * * * * * ** ****** * * *
* ** ** *** ***** * = 2; i <= sqrt(n); i++)
* * * ** * * * *
*** **** * * * * ** **** * * * ** (n%i == 0)
* * * * * * ** * *
* ** * * * * ** * **
** * ** * * *** * (i <= sqrt(n))
* * * * * * * ***** * ** * a * *** **
* * * * ***** *
* ****** **** **** * ** * a * * *** *
*** *** * * ** **** **** ** ** ** **

return 0;

}
answered by (-98 points)
0 like 0 dislike
Hidden content!
#include * * * * ****

#include * * ** ***



int main()

{

int i,n;




*** *** * * * **** **** * ** **** * * ****
*** * *** * *** = 2; i <= sqrt(n); i++)
** * ** ** *
** *** * * * ** * * (n%i == 0)
** * * *** * ** **** * **** *** **
* ** ** * * ** **
* * ** **** *** (i <= sqrt(n))
** ** *** ** * ****** a * * * *
** * ** ** ** ***
** * * ** ** ** * ** * ** ** a ** * * * *
* **** * **** * ********* **

return 0;

}
answered by (-98 points)
0 like 0 dislike
Hidden content!
#include<stdio.h>

#include<stdlib.h>



int main()

{
**** **** ** *** ** * n,i;
* ** * * * * **** * * ** *** * ***
* * **** ************* * **** *
****** * **** * *** ** * **
* * * *** ** ****** * ** *
******** * * ******* * * ** * *** ** **
** * ** * * * * * *
* ** ** * ** * * *** * * * * **** * * *** ***** * * * a prime");
* * * ** * * * ** * * * *
* * ****** * * ******* *** *** *** * **


* ***** ** * *** 0;



    }
* * ** ** ****** ** *** ** * ** a prime");
* * * ***** ***** ** 0;



}
answered by (-248 points)
0 like 0 dislike
Hidden content!
#include<stdio.h>

#include<stdlib.h>



int main()

{
* * *** * * *** * * * * n,i=2;
*** *** * * *** ** * * * * * ****
* * ***** * * * ** ******* ***
* ** *** *** *** ** *
*** * *** * * * *** * **** * ** *
***** ** ** * * * ** * * *** * * **
* ** ****** * * * * ** * ** *** **** * * *** * ** * ** * *** * * a prime");
** ******** ** ***** **
* * ** * *** ** *** * * * *******


* *** ** * * ** * * * 0;


** ** * * * ** *
* ** * * * * ** * a prime");
** ** ** ** * * ***** *** 0;



}
answered by (-248 points)
0 like 0 dislike
Hidden content!
#include<stdio.h>

#include<stdlib.h>



int main()

{
* ** **** **** ** * ****** n,i=2;
* * * * * *** * * * * * * ** ** *
* * * **** * ***
** * **** * **
** ****** * ***** * *** * * ***
** ** ******* * * * ** **** ** *
* ** * ** * ** *** *** **** * * * * * **** * a prime");
* ** ****** * * ** ** ** * ** **


****** * ** * ** * ** * 0;
** ** *** * ***** ** **
* * * *** *** ** ******
* **** * * *** ** *** * * * * a prime");
* ** * *** * * * ** 0;



}
answered by (-248 points)
0 like 0 dislike
Hidden content!
**** * * ** * *
* * * * * * *



int main()

{
* * * * ** int i,j,k;
* * *** * * * * **** * ** * ****
* * * * k=0;
* **** * *** * * ***
* ** * ** {
* ** * * *** ** * * * ** **** *
* * * ****** * **   k=1;
** ** * }
** * ** * * if(k==1)
*** ** * * * * * ** ** ** *** * *** a ** *** **
**** * * * * *** * * ** * else if(k==0)
** * * **** * ** * *** *** ***** a * * ** **
* * * * return 0;

}
answered by (-158 points)
0 like 0 dislike
Hidden content!
#include *** ** ** *



int main()

{


* ** * ** ** * i, n, k = 0;


** ** * ***** * ** ** * &n);




***** = 2; i < n; i++)
* ***
* * * *** **** * **** * % i == 0)
** * * *** ** * *****
** ****** *** * *** ****** * * ** *** *** ** * * ** * = 1;
* ***** * *** * ** * ** *** ** *** ** ** ** * * *


*** * * ***** * * * **
** *






* * == 0 *** * * * n > 1)
*** * *** ** *** ** a *** * * *
** ** ***
* **** * ** ** * * * ** *** * * * a * * **** ****


****** 0;

}
answered by (-120 points)
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.126.15
©2016-2025

Related questions

0 like 0 dislike
64 answers
[Exercise] Coding (C) - asked Oct 13, 2016 in 作業 by Shun-Po (18k points)
ID: 13763 - Available when: Unlimited - Due to: Unlimited
| 6.1k views
0 like 0 dislike
61 answers
[Exercise] Coding (C) - asked Oct 13, 2016 in 作業 by Shun-Po (18k points)
ID: 13736 - Available when: Unlimited - Due to: Unlimited
| 6.4k views
0 like 0 dislike
91 answers
[Exercise] Coding (C) - asked Oct 13, 2016 in 作業 by Shun-Po (18k points)
ID: 13617 - Available when: Unlimited - Due to: Unlimited
| 9.2k views
2 like 0 dislike
44 answers
[Exercise] Coding (C) - asked Nov 2, 2016 in 作業 by Shun-Po (18k points)
ID: 16861 - Available when: Unlimited - Due to: Unlimited
| 5.3k views
1 like 0 dislike
129 answers
[Exercise] Coding (C) - asked Oct 19, 2016 in 作業 by Shun-Po (18k points)
ID: 14119 - Available when: Unlimited - Due to: Unlimited
| 11.7k views
12,783 questions
183,442 answers
172,219 comments
4,824 users