0 like 0 dislike
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: 13427 - Available when: Unlimited - Due to: Unlimited
| 1k views
0 0
Called for Help

67 Answers

0 like 0 dislike
Hidden content!
#include * * * * ****

#include *** ***** *



int main()

{
** ** * * *** * * x, y, z=0;
* *** * * * ** * * * * * &x);
* * **** ** *** = 2;y < x;y ++)
**** * ** *
* **** ***** *** * * * ** % y == 0)
** **** * *** ****** * * ** *** * * *** * *** * ** = 1;
* ** * ***** **** *
*** ** ** *** ** * * *** == 1)
* *** * * * * ******* * * * * ****** ** a prime");
** ** ** ** * * **** *** **** a * *** **


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

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



int main()

{

    int a,i;

    
* * * ***** ** ** *** * ** ** *** ** *
** **** ** ** ** * * * *** = 2 ; (i*i)<=a ;i++)

    {
* * ******** * * ** * *** *** * *** * *** *
* * *** * * ** *** * ****** **** ** **
** ** ***** ******* *** * ** * * * * ** *** * ** * **** ** **** * a prime");
* ** ** * * *** * * ** ** ***** *** ** * ****** **** **** * ** ** * **
*** ** ***** * * * * * ** * * * ** * ** ** **** *** *** ** * 0;
**** **** ** ** ** * ** * **** *
* ***** *** ** * ** *** ****** * * ****
** * * ** *** * * * *** * *** a prime");
*** * * * ** **** * ** * * ** **** * ** *** *
** ** * **** ** * ** **** * 0;
* * * ** ** ** * ***** *** * *** ** * * *** * *** * **
answered by (70 points)
0 like 0 dislike
Hidden content!
#include <stdio.h>

#include <stdlib.h>

int main()

{
* *** ** *** * * ** * i;
*** * * ** ****** * * n;
* ** * * * * ** ** *** * j=0;
* * * * * * * * * * ** * * **** ** &n);
* *** ** * *** * ******* (i=2; (i*i)<=n; i++)
* ** * ** **** * * **
** * ** *************** ** * * * ((n % i)== 0)
* * ** * *** ** ** * ***** ** ***
**** * ****** ** ** * ** * *** **
** * ****** *** ** * ** * ** * * *
**** ** *** *** **** * * * * *** * **
* **** * * ** ** **
*** * * *
* * * ** *** ***** * == 1)
*** * * * ** *** ** ("not a prime\n", j);
* ****** * ** * *** * if (j == 0)
* ** * ** ** * * * * ** * *** a prime\n", j);
*** **** * * * *** * *
* ** *** ** ** ** * * * ***
* * ** ***** *** **** 0;

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

#include * * **



int main()

{
* **** * * ********* x, y, z=0;
******** ** * * * * * * *** * input a ** *** **
* *** ** * * ****** * **** * * * &x);
* * *** * ** * = 2;y < x;y ++)
** * * * ** **
**** ** * * ** * * *** * *** * % y == 0)
*** ** *** ** * * * * * *** * * * * *** * *** ** = 1;
* ** *** ** * * *
* *** * * * *** * * == 1)
* * ****** **** * * **** * * * *** *** *** * a ** *
* * ** *** ** * **** * * a prime");


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

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

#include <stdlib.h>

int main()

{

    int a,j;
* ** **** * * * ** **** *** ***** * **
***** ** ***** ** ** ** ** *** * * * *

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

    }    

    printf("is  a prime");
*** ****** **** ** * ** * * *

    return 0;

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

#include <stdlib.h>

int main()

{
* * * ** * *** *** ** * num = 0 , i = 2 ;

    
** ** ** ** ** * ** * * **** ***** ** * * *
***** * * * ** ** = 2 ; (i*i)<=num ;i++)
* ** * * ******** * **
* * * *** * ** ** * * * ** * * ****
* *** ** ** * ** *** ** * *** *****
****** ** **** * *** * *** ** ****** **** *** * ** * * ** *** * * * ** a prime");
*** **** ** * * *** * * ** ** * ** * * ** * ***** ** *** ** * ******** ****
* * ** * * *** *** * **** * ** * * ** *** 0;
* ** ** ** * ** ** *** *** ** ** *** **** *
* * *** * ******** * *** *    
* * *** ** * *** * **** * * * ** * * a prime");
* * * * * *** * *** * ** * * ** * ***
* **** ** * ** *** * * ******* 0;

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

#include ***** ** *



int main()

{
*** * * * ***** num, test, testNum;
** **** ***** **** ** ** * * &num);
* * * * * * ** * * (testNum = 2; testNum < num; testNum++)
* **** ***** **** * * *
* ** * **** ** ** ** ** *** * ***** *** *** = num % testNum;
* ** *** ** * * * * ** ** **** *** * * * (test == 0)
* * * **** ** *********** ** * * **
*** * ** * * * * ** * * * * ** * ** * * * ** *** * ** ** * ** * ** * ***** a prime\n");
* * * ** * * **** ** *** * ** * ** * ** * ** ***** ** ******* * * 0;
**** * ** ** ***** * *** * ***** ** *** **
* ** * * * * **** **
* * *** ***** *** * * **** ** * a prime\n");


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

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

#include * **** * ** * *



int main()

{
* * * * * ** ** num, test, testNum;
* ** ** ** ** **** ** * ** * * * * * ** &num);
*** * * *** * ** ** * (testNum = 2; testNum < num; testNum++)
* * *** * * *
* ******* * * * ** * ** * ***** ** ** * * = num % testNum;
**** ** * * *** * * ** * * ** (test == 0)
* ** *** * * * * * ******** ** * * *** ** *
*** * * * * ** * **** *** ******* *** * * * * ***** * ** ** * * a prime");
**** * ***** * * * * **** ** * * * * * * * * ** *********** ***** 0;
* * *** * * ** * ** * * **** ** * * *** **
** ** ** * * ** * * * ** * **
**** *** * * * * *** * * *** ** * a prime");


* * * *** ** * * * 0;
answered by (-214 points)
0 like 0 dislike
Hidden content!
* *** ** *** *** **

* ** ** *****



int ***

{

int *

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

* **** * * * *

{

**

**

}

* **** * ***

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

*

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

*** * ** ** ***

* * 0;

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


**

{

int i, n;
**** * **** * * * * input a *** *** *
* * ** * *** *** ****
* * * *** * i++)

{
** * == 0)
* * *

}
* ** n)
** **** * Not * * ** *** * *
* if(n * *****
** *** *** Not * * ***** *

else
** ***** Is * * * * * **
** * ***** * *
**** 0;

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

Related questions

0 like 0 dislike
23 answers
[Exercise] Coding (C) - asked Oct 13, 2016 in 作業 by Shun-Po (18k points)
ID: 13528 - Available when: Unlimited - Due to: Unlimited
| 450 views
0 like 0 dislike
45 answers
[Exercise] Coding (C) - asked Oct 13, 2016 in 作業 by Shun-Po (18k points)
ID: 13501 - Available when: Unlimited - Due to: Unlimited
| 847 views
0 like 0 dislike
26 answers
[Exercise] Coding (C) - asked Nov 3, 2016 in 作業 by Shun-Po (18k points)
ID: 16957 - Available when: Unlimited - Due to: Unlimited
| 556 views
0 like 0 dislike
37 answers
[Exercise] Coding (C) - asked Oct 20, 2016 in 作業 by Shun-Po (18k points)
ID: 14579 - Available when: Unlimited - Due to: Unlimited
| 684 views
0 like 0 dislike
73 answers
[Exercise] Coding (C) - asked Sep 29, 2016 in 作業 by Shun-Po (18k points)
ID: 11603 - Available when: Unlimited - Due to: Unlimited
| 1.2k views
12,783 questions
183,443 answers
172,219 comments
4,824 users