0 like 0 dislike
7.3k views

寫一個判斷是否為質數的程式

We have the uncompleted program below:

#include <stdio.h>
int is_prime(int n)
{
   /*INSERT YOUR CODE HERE*/

   /*在這裡寫你的程式*/
   /*END OF YOUR CODE*/
}

int main(void)
{
   int n;
   scanf("%d", &n);
   if (is_prime(n))         // function return 1
      printf("Prime");
   else                     // function return 0
      printf("Not prime");
   return 0;
}

Please complete this program by only insert your code between those tags:

   /*INSERT YOUR CODE HERE*/
   /*END OF YOUR CODE*/

Remember: prime number is a whole number greater than 1, whose only two whole-number factors are 1 and itself.

You may correct the cases, but your code always be revised!

[Exercise] Coding (C) - asked in Chapter 9: Functions by (5.2k points)
ID: 35783 - Available when: 2017-12-07 18:00 - Due to: Unlimited

edited by | 7.3k views

70 Answers

0 like 0 dislike
Hidden content!
#include <stdio.h>
int is_prime(int n)
{
* ** **** *** * **** YOUR CODE HERE*/

*** ** * i;
* **** * *** ** * * 0;}
** **** *** * * **
**** ** * * * ** ** *** *
   {
* **** * * * * *** ** ** * ** * prime
** ** * * * ** * ***** ** * * ** *** * * * **
** ***** ** * ** * ** * * *** ** ** * * 0;
* ** * * * * ** ******* * * ** ** ** **
** * * ** ***** *
* * ** * ** * * ** * * 1;
* ** ** ** ** ** * *
* * *** * ** * OF YOUR CODE*/
}

int main(void)
{
* ** ** * * * n;
** * * * *** ** * **** * * **** * &n);
   if (is_prime(n)) * ** * **** * ******* **** * ** **** function return 1
**** ** * ** * * ********* * * * *** ****
** ******** ** ** ** **** ***** *** *** *********** * * ** * * * * * **** *** *** ** *** * * * * * ******* * ** function return 0
* * ** ** * * * * * ** * ***** ** **** * prime");
* *** *** ** 0;
}
answered by (-284 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Wrong output
Case 5: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int is_prime(int n)
{
* * ***** *** * * YOUR CODE HERE*/
   int i,flag=0;
   if (n<=1)
   {
**** * *** ** * *** *** ******* * ** ** * 0;
   }
* * * * *** *** *
*** ** * * *** *** ** * ***** * ******* * * (i=2;i<=n/2;i++)
** * * * * * * * ** ******* *** * *** **
**** *** * * * ** * ** * **** ** * * * * ***** ** * ** ** * * **
* **** *** ***** * ******* * * * * * **** * ***** * * (flag==0 && n!=i)
** * **** * * * ******* ** ***** * * * * ** ** * * * ** * ****
*** ** * ** * ** ** ** * * ** ** * *** * * *** * ** * * ** ** *** * *** * * ** 0;
* *** * * ***** * ** * * ** * ** ** ** * ** * * ** ** *** * ** * * * **
** * * ** **** **** * *** *** ** ***** ***** ** ** ** **** * ********
* **** * ** ** **** *** *** * ** ********** * *
* * ** **** ** ** * * * * *** * * * ***** * (flag==0 && n==1)
* *** * * ** *** * ** * * * * *** *** **
******** * * * ** ** ** **** ** * *** * ** * *** ** *** * * ** * 1;
*** * * * **** *** ** * ** *** *** ** * * ** **
   }
**** * *** * * ** OF YOUR CODE*/
}

int main(void)
{
** *** ****** * * n;
* * * * *** ** * * ****** **** * * &n);
   if (is_prime(n)) * * *** ** ** ** ***** * ** *** ****** * * function return 1
** * * * * ** ** ** * * *** * **** * *****
************ * *** * ** * * ***** * * * * ** *** ** * ** ** ** ** ****** * * ** * * * ***** *** * * **** ***** * * **** * * * function return 0
* * ****** *** *** * ** * ** ** * ** ** * prime");
** ** *** ** ** * 0;
}
answered by (-249 points)
edited by
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Wrong output
Case 5: Correct output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
Case 5: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int is_prime(int n)

{
** * ** * * * * * *** flag=1,i;
***** ******** * ** ** *
* * **** * ** *** * * ** * ** *
* * ****** ** * ***** * * **** **** * ** ** 0;
* * * * ** * ** ****
**** * ******* * * * i<=n/2; i++)
   {
**** ****** *** * **** * * **** *** ** *******
** ** * ****** **** *** * * ** ***
* * * *** ** * * * * * *** * **** * ** * * * **** * * ** * * **
* ***** * ** ** * * * * * * ** * * ** ***** *** **** * ***
* *** * * *** ** ** * *** ** * * * **
   }
** * * ** ** * ** * ******* *** ** *** flag;
}

int main(void)
{
** *** * **** n;
*** ***** **** ** * * ** *** * &n);
   if (is_prime(n)) * ** ** * * * * *** ****** * * ** * * *** * *** function return 1
* * * * ** * * * ** * * *** ** ** *** *** *******
** * ** ****** * * * * * * * ** * * * *** **** * ***** *** * * ** *** * *** * * *** ** ** ** * * *** **** * ** function return 0
* ******** ** *** ** * ** * ***** **** * ***** prime");
*** * ** ** ****** 0;
}
answered by (-281 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
Case 5: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int is_prime(int n)
{
** ******* * *** i;
* *** * * ***** (n<=1)
* * * * ** * *** *** * 0;
* ***** * * * *** (i=2;i<n;i++)
   {
* * *** * * * * ** * *** ** ** **** *** (n%i==0)
****** * * ** ** ** ** *** * * * *
** ** *** * ** *** * **** ** * *** ** ** **** ** * ** * *** 0;
*** * ** * * * * *** * ** * **
* * ** * *** ** * * ***** * ** * ** * *
** * * ** ** **** * * ** ** * **
* ****** * * * ** * * ** ***** * * * * **** ** *** ** * * 1;
** * *** ** * **** * ** * *** **** *
   }
}

int main(void)
{
* * * * **** * * ** n;
* * * * * * * * * **** * *** ****** *** &n);
   if (is_prime(n)) *** * * ** * * ** ** *** ** ** ******* function return 1
**** * **** ** * * * ******* ** * * * ***** * * *
** ** * ** * ** * ** * * * * * * * *** ** * * * * * ** * *** ** *********** * ** * * * ** * * *** * * * * * * **** ** function return 0
* ** * **** *** * ** **** * ** * * ** * *** prime");
* * * * * ** * * 0;
}
answered by (-229 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
Case 5: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int is_prime(int n)
{
** * ******* * ** *** YOUR CODE HERE*/
** * * ** ** * ** * i;
****** ** *** ** * * **** *
*** *** ** ****** * * ** 0;
****** * * ****** * **** * *
* * * * ** *** *** * * * * ** *
* * * ** * * * ** * * *** ***** * * * **** * * * ** ** **** 0;
** * ** * ** **
* ** * **** ** ** *** 1;


***** *** * * ** * ** *
* ** *** * * **** * OF YOUR CODE*/
}

int main(void)
{
   int n;
* ***** ** * *** **** ** ** ** &n);
   if (is_prime(n)) ** * ** ** *** ** * ***** * * * ** * function return 1
** * * * *** * * ** *** * ** **** * ******
*** ** ** * ***** * ** *** * * * ** * ** ** ******** * * ** * * ******** ** * * * * * *** * * **** ***** *** * *** **** function return 0
* * * *** * * *** *** *** * * prime");
**** ****** ** * * 0;
}
answered by (-329 points)
edited by
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
Case 5: Correct output
0 0
Case 0: Wrong output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Wrong output
Case 5: Correct output
0 0
Case 0: Wrong output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
Case 5: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int is_prime(int n)
{
** * * ** ****** * i = 2 , prime = 1;
*** * ** * *** * ** (n<=1)
* ** * * ****
* ** **** **** * * * * * * ** * **** * ** * * *** 0;
   }

* ** * * *** ** * ** **** i<n; i++)
   {
** * ** ** * ** * *** * ** ** * ** (n%i==0)
** *** * *** * ** * * ** ** * * * * *
* ** * ***** ** * * ******** * *** * * * * * * * ***** ***** ****** * = 0;
** **** * ** * * ** * * * *
*** *** ** * * ** ** ** * * ** * *** prime;
* ** * *** **
}

int main(void)
{
* * * * ** * * *** n;
** * *** ******* * ** * * *** ** &n);
** * ****** * *** *** (is_prime(n)) ** ***** * * *** * * ** *** * * ** * * **** * function return 1
** * *** * **** * * ** * * ** *** ** * *** * ***
* *** * ** ** **** * ***** *** *** * ** *** * *** **** * * *** ** ****** ** * ***** * *** * * * * * *** function return 0
* ** ********* * * * *** * * * * * * ** * ** prime");
* ** ** *** **** ** 0;
}
answered by (-127 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
Case 5: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int is_prime(int n)
{
* ** ** * * * i;
**** ** **** * ** ** * * || n<=0) return 0;

* *** *** ** ** ***** ** *
** *** * ** **** **
** ** *** ** * * * * ** * * *** *** * *** * * **
* * * * *** * ** * *** *** * **** ** * *** ** ** *** *** ** ** **
*** * * * * * * * * *** * **** * *** ** * * * *** * * ** * * * * * ** * * ***** return 0;
* * ** * * *** * ** * * * * * * * *** **** *** ** * * ** **
*** **** * ** ** ** **** *** * ***** * ** * ** ***** **** * * * 1;
** ** * * * * * * **

}
int main(void)
{
   int n;
* * ** ** * ** ** *** *** ** &n);
   if (is_prime(n)) ** * * *** * * *** *** * ** * * ** function return 1
*** * * ** **** * ** **** * *** *** * * ** * * *
   else * * *** *** ******** *** * ** * ** * ** ** *** ** ** *** * * * * * ** ** ** * * ** * * * ** * * * *** * ** *** * function return 0
* * * * * * * * * * * * *** * * * ** ** prime");
* * * ** ** * * 0;
}
answered by (-301 points)
edited by
0 0
Case 0: Correct output
Case 1: Wrong output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
Case 5: Correct output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
Case 5: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int is_prime(int n)
{
** *** * i = 2 , prime = 1;
**** * * ** (n<=1)
* * ** ** * * * ** ** ** 0;

** * ***** * i<n; i++);
   {
**** *** ** ** *** **** * ****** * (n%i==0)
* *** ** * * * *** **** * ** * * ** * * * *
*** *** * ** * * * ** * *** * * * * *** ** ** ** ******* ** = 0;
* *** ** * * ***** * *** * * **** **
**** ** ** *** *** ** * ** * * * * * * * prime;
*** ** ** ** *
}

int main(void)
{
** * * * * n;
** ** * * * * * * * * * &n);
******* * * **** (is_prime(n)) * * * * * * ****** * ** ****** ** ** * function return 1
*** * * * ** **** * ***** * ****** *** * *** * **
*** ** ** ** * * ** *** * * **** * * ****** * * * **** * *** ** ** **** ** ** * * * ** *** ** * ** ******* ** *** * function return 0
** ** * ** **** * **** * *** *** ** ** * ** ** prime");
*** * * * * * ** * * 0;
}
answered by (-127 points)
0 0
Case 0: Correct output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Correct output
Case 4: Correct output
Case 5: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int is_prime(int n)

{
** * * *** * flag=1,i;
* * * * * * ** ** i<=n/2; i++)
   {
** * * * * ** * ** ** *** * * *** * ** ** * *
* *** **** * * ***** * *** * * * ** *
*** * **** * * * * * **** * ******** *** *** * * * ** ** ** *
*** * * * * ** * **** ** * * * *** ** **** * * ** *** ** *
* **** * ** * * * **** ** ****

* *** ***** *** **** * ** flag;
}

int main(void)
{
** * *** * ** *** * n;
** *** ** * * ****** ** ** *** ** * &n);
   if (is_prime(n)) * * ** ** * * ** * * ** ******** * function return 1
***** ****** *** ** * * ** * ** ***** *** * * * * ***
* **** ***** * *** ** ** * **** ** ** * * ** * * ** ** ** ** *** * * * ** * ** ** * * * * ** * * * * * * *** * ** ** function return 0
* ***** ** * * **** ** **** * * ** * ** * prime");
****** ** *** ** * 0;
}
answered by (-281 points)
0 0
Case 0: Wrong output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Wrong output
Case 5: Correct output
0 like 0 dislike
Hidden content!
int is_prime(int n)
{
* **** *** * ** ** flag,flagb,i;

*** ** * *** * * ** * * (n==1 || n==0)
* * ** * *** *
* *** *** ** * * *** *** ***** ** ** ** 0;
   }
*** *** ** *** * **
**** ** * ** * ** *
   {
* ******* ******* ** * * * ** * * * *
**** * * ** **** ** * * * * * * * ** *
** ** * ** ** * * *** * *** *** * **** (flag==0 && n!=i)
* * ** * ** * ** *** * * * *** * *
** **** * *** * ** ** ** ** * * * ** *** *** * ***** * * * * 0;
*** * **** * * ****** **** * * * * * *
   }
* * * * * * **** * (flag==0 && flagb==1)
* *** ** * *
**** * ** *** * * * ** ** * * ***** 1;
* * *** * * *
}

int main(void)
{
* * **** * * * n;
* *** ** ** * * ***** * * **** * ** ** ** *** ** ** &n);
**** * * * ** * * (is_prime(n))
*** ***** * * **** *************** **** * ****** *** ** ****
**** ** ** ***
*** * ** ** * ** * * * ** *** * * * * prime");
** * * *** * 0;
}
answered by (-249 points)
0 0
prog.c: In function 'main':
prog.c:28:7: warning: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]
       scanf("%d", &n);
       ^~~~~
prog.c:28:7: warning: incompatible implicit declaration of built-in function 'scanf'
prog.c:28:7: note: include '<stdio.h>' or provide a declaration of 'scanf'
prog.c:30:7: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
       printf("Prime");
       ^~~~~~
prog.c:30:7: warning: incompatible implicit declaration of built-in function 'printf'
prog.c:30:7: note: include '<stdio.h>' or provide a declaration of 'printf'
prog.c:32:7: warning: incompatible implicit declaration of built-in function 'printf'
       printf("Not prime");
       ^~~~~~
prog.c:32:7: note: include '<stdio.h>' or provide a declaration of 'printf'
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.69.17.13
©2016-2025

Related questions

0 like 0 dislike
47 answers
[Exercise] Coding (C) - asked Dec 7, 2017 in Chapter 9: Functions by semicolon (5.2k points)
ID: 35785 - Available when: 2017-12-07 18:00 - Due to: Unlimited
| 4.9k views
0 like 0 dislike
45 answers
[Exercise] Coding (C) - asked Dec 7, 2017 in Chapter 9: Functions by semicolon (5.2k points)
ID: 35784 - Available when: 2017-12-07 18:00 - Due to: Unlimited
| 5.5k views
0 like 0 dislike
99 answers
[Exercise] Coding (C) - asked Nov 2, 2017 in Chapter 6: Loops by semicolon (5.2k points)
ID: 29829 - Available when: 2017-11-02 18:00 - Due to: Unlimited
| 11.2k views
12,783 questions
183,442 answers
172,219 comments
4,824 users