0 like 0 dislike
5.2k 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 | 5.2k views

70 Answers

0 like 0 dislike
Hidden content!
#include <stdio.h>
int is_prime(int n)
{
* * *** * * flag,i;

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

int main(void)
{
*** **** * * * * * n;
* * * **** ***** *** **** ** &n);
* * * * * ** *** ** *** (is_prime(n))
** ** *** * * * * * ***** *** * * * ****** * **
** **** *** **** *
* * *** * * * ********** **** ***** * * * * prime");
* ****** *** ** * ** 0;
}
answered by (-249 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!
#include <stdio.h>
int is_prime(int n)
{
** * ******* ** * ** YOUR CODE HERE*/
*** * * * * * * **** *** i,prime=1;
* * * ** *** ** * * ** *

* *** * ** * * * **** *** * ** ** ** * * * * * *** ** && n!=i){prime=0;}


*** ** ** * ********
* * * * ** * * * * * **
** ** * *** *** * ** * prime;

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

int main(void)
{
**** * ***** * ** * n;
* * ** * ** *** **** * * * &n);
* ** **** * * * * (is_prime(n)) ** * * * ** * *** * ******* * ** ****** function return 1
** **** * ** * * **** *** * ** *** * *** * ****
* ** ** *** *** * ** ** *** * ** ** **** * * * * * ** ** * * * *** * ** * *** * * ** * * ***** ** * *** ** * * * * * ** function return 0
* * * **** * *** ** * * * * *** * * **** ** prime");
* * * * * * ** * ** 0;
}
answered by (-286 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;
**** * * * * * *** **** ** *
** ***** ** ** *** *** ** **** * ** *** ***
* ** * * * * * * **** ** * * ** ** ** * ** *
}

int main(void)
{
* ** ** * * * n;
****** * ** * ** * ****** * * ** ** &n);
   if (is_prime(n)) * * * * *** **** * ** *** ** * * function return 1
** ** * * ** * * * ** ** * * * ** ** **** *** *
* ******** * * **** ***** * * * * ***** * ** * * ********** ** * * ** * * ** *** *** * **** * * * * ****** * **** ** * * function return 0
* ***** * * * *** * **** ** ****** * ** * ** prime");
* * * * * 0;
}
answered by (-32 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!
#include <stdio.h>
int is_prime(int n)
{
**** * *** ** ** ** flag,i;

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

int main(void)
{
** ** * *** ** * * n;
* * * * * ** * ** * ** * * * * * &n);
   if (is_prime(n)) ** ** * ** *** * ****** ** * * * * * ** ******
* ** * * ** ** * *** ****** ** * * * ** * ** *** **
** **** * * **** ** * * ** * * * ** * ** * * * ***** ** ** ******* * * * * * * *** ** ***** ** ****** * **** * *** * **
*** ********* **** * * * * * * * *** * * ** prime");
* * * * ********* 0;
}
answered by (-249 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!
#include <stdio.h>
int is_prime(int n)
{
** ** ** * ** ** *** * a;
** * ** * *** * *** * ** ** * ****
   {
** * ** * * * * *** ** * ** * * *
** ** *** * *** *** ** ** ** ***** ** ** * ** * 0;
* * * *** * ** *** ** * **** * ** * * * ** * **
* ** **** * * *** * * * * ******* * * * * **** 1;
   }
}

int main(void)
{
* * * * ** n;
* ** ** *** ***** * * ** ** * * &n);
***** ** ** ** (is_prime(n)) ** ** * ** ** ** * ** **** ** ***** * function return 1
* * *** *** *** ** ** *** ** ** *** ** * ** *** *
* ** * ** ***** * * ** * * * * *** ** * * * * * ** * * **** ** * * * * * * * ** * * * *** *** ****** ** **** *** ** *** * function return 0
* * * **** ** * * ** ****** ** * * **** * * ** prime");
* * * ** *** * *** * 0;
}
answered by (-498 points)
0 0
Case 0: Wrong output
Case 1: Wrong 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)
{
* * * **** * ** * *** ** j;
*** ** ** *** ** * ****** * * * {
** *** ** * * * *** * * * * ****** * ** ****** break;
** ******* **** * ** **
*** ** ** * * * ** **** *
*** *** * ** ** ****** **** ** ** * * * * 1;
* *** *** * * * *** * ***
* * * * * * ** * ** * * ** {
* ***** **** * * ** * ** ** ** * ******** * *** 0;
** * * * ** * * **
}

int main(void)
{
   int n;
* * ** ** ** * * ** * * * * * &n);
   if (is_prime(n)) * ** * * ******** * * * * * * * * ** * *** function return 1
* *** ** ** * ** ****** * ** * *** * * ** * ***
** ** ** * * **** * * * **** ** ** * *** * * * * **** *** ** ******** * *** * *** *** ** ** ** * * ** ** * *** ** *** ** *** * function return 0
** * *** * ***** * * * * ** * *** *** * prime");
* ******** ***** ******* * 0;
}
answered by (-120 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;
** * * * ** * ** * ***
**** * * * * ***** * * ** * ** * * *** * ** ** 1;
******* ** * * **** * *** * ** **
** * ** ** * ** ****** * *** * ** ** **
* * * *** **** * * ** * ** ***** ****** * ** * * * ** **** ** * * * 1;
* ** * ** * **** * * * ** ** ** * ** **** ** * * if(n%i==0)
** *** ** **** * * ** * * ** * ** * ** ** * ** ****** ** * * ** 0;
** ** ** * ** * ***
}

int main(void)
{
   int n;
** * * *** * ***** * *** ** &n);
   if (is_prime(n)) * ** ** *** ** *** *** ****** * ** *** * * function return 1
* *** ** * * * *** **** * *** ** * * * *** * ***
   else ** * ******* * *** * * * * * * **** * * * * ** *** * * * ** ** * ***** * **** ** ** * * * * ** ***** function return 0
* * * *** * ** **** *** ** * ** * * * * prime");
** * ** * * ** * * * 0;
}
answered by (54 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 * *** ** n) {
** ** * * ** int flag=0;
* ** * ** * * * i=2; * ** * *
** ** * * * * ** *** * * if * * *
* * * * ** ** *** ** * ** * * * * flag++;
** * *** * * ** * * ** ** * ** * break;
*** ** * *** *** * **** }
******** *** * ** }
*** * ** return !flag;
}
int * * {
* ** ****** int n;
* ** * * * *** ** * *** *
* * * *** if * **** ******* * ** *
** ** **** *** else ** * * * ** * **** *
**** * * * ** * return 0;
}
answered by (-116 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 * * ** *** n) {
**** ***** int flag=0;
*** * ** ** *** i=2; * **** * *
* * * * * **** **** * * if ****
* **** * ** * **** *** * ** ** * ** flag++;
*** * *** * **** **** ** *** * ** break;
* * * ** * ** * ** ** }
* ** ** *** * }
* ** * * *** return !flag;
}
int * * {
**** * *** * int n;
* *** * * * *** * ** * * *
* * ***** if * * * ** ** * *** * ** **
* * * * ** else * * * *** * * * *** * *
** **** * * ** * return 0;
}
answered by (-116 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!
#include <stdio.h>

int is_prime(int n)
{
* * * * * * i;
   
*** ******* *** * * **
* * **** *** **** ** * * * * ** **** ** ** * 0;
** * * ** * ** ** * * ** * ** ** *
* * **** ****** * * *
** * * * * * * * * **** *** * * **** *** * *
** * * * * * * *** **** * ** * * * ** 0;
** ****** * ***** **
***** ** * ** * * ** ** * * 1;
}

int main(void)
{
* * * * ** ** * n;
   
* ** *** * *** ** * * *** &n);
   
* *** ** *** *** * ** (is_prime(n)) ** ** * **** * ** *** ****** ** *** *** ** * ** function return 1
* *** * ***** *** * * * ** * ** *
* * * * ** ** ** **** ********** ** * ** *** * * ** ******* ** * * * * **** * ** * * ** *** ** * * * ** ** **** *** * function return 0
** ** * ** **** *** *** *** ** ** ** ***** * prime");
*** * * * ** * ** 0;
}
answered by (1.1k 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
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.100.224
©2016-2024

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
| 3.6k 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
| 3.8k 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
| 7.7k views
12,783 questions
183,443 answers
172,219 comments
4,824 users