0 like 0 dislike
26.6k views

Write a program that get the input of two integers n and m(n < m). Find out all prime numbers between n and m (not include n and m), then display them on the screen.

寫一個程式 輸入兩個整數n和m (n < m),找出所有n到m之間的質數(不包含n和m)。

Example input 1

1 10

Example output 1

2 3 5 7

 

Example input 2

10 20

Example output 2

11 13 17 19

 

[Exercise] Coding (C) - asked in Chapter 6: Loops by (5.2k points)
ID: 29829 - Available when: 2017-11-02 18:00 - Due to: Unlimited

edited by | 26.6k views

99 Answers

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

int main()

{
    int n,m,i,flag,count=1;
    scanf("%d %d",&n,&m);

    if (n<m)
    {
        for (n=n+1;n<m;n++)
        {
* **** * ** ** *** * **** ** * ** * * * ** **** *** * * * (i=2;i<=n;i++)
** *** * ** * ** ** * * * * * * * ** ** * * *** * ** *** ** * *
***** ** ****** * **** **** * ** *** *** **** **** *** * * *** * ** ** * ** *** * ** ** *** *** * *
* * ** * *** * ** * * * * ** * * *** ***** **** * * * *** * * * * * * * (flag==0 && n==i)
* * *** * * * * * ** *** *** * * ** * * **** ** *** ** *** *** ** ** * ** * * **
*** * * * * ********* ***** ** * * * * ** * * * *** ** ****** * *** * * *** * * ** *** * * ** ** * (count==1)
** ** * * **** ** ** *** * * * ***** * * * *** * ** * * * * * * **** *** * ** * ***** * ** **** * * ************ * **
* ** **** * ** * * ** * * ** ** * * * * * * * * * * *** * * * ***** ** * ** *** *** * * ** ** * **** * * * * * * * * *
*** *** * * * **** * *** ** * * *** *** * *** * * ** *** * * * * * * * *** ** * ** ** * ** *** * **** *** * ** * ** *
***** * * ** * ******* ** * ***** * * * * ** * ***** * *** ** **** *** **** * ** * ** * ** *
*** **** ** ** * * **** ** * *** * * * ******* **** * * * **** * *** ** * *** *** * ** ** * * * **
* ** ** * * *** *** *** ** * ** *** *** * ** *** * ** * *** ********* **** **** * * *** * * * * *
** **** ** * *** * * **** **** * ** ***** * * * * ** ** ** * ** * * * ** * * * *** * * ** * ** ***** ** * * *** ****** ** * %d",n);
** ** * * ** * * ** ** * ** ** ** * ** *** * ***** ** * ** *** * * *** ** * * ** **** * * ** ****** * * ** ** *** *** * ** * *
* * ** * * ***** ** **** ** * ** ** ** * ****** *** * ***** **** * * ** * *** * * * * * ** * * * * ** * *
**** * ** * * ** * ** *** * * ** ** *** *** ** ** * ** * * * * * *** *** *
**** * * * ** * * * *** * * * * ** ** * ** ** * *** * * *** *** * * *** * ** * *** (flag==0 && n!=i)
*** ** * ** * * * ** * ****** * ** *** ** **** * ***** * **** * ** ** * ** * * * * * * ***
** * ***** * *** * ** * ** * ** * * * *** **** * ** * ** * * * *** * **** **** ** ***** ** * * **** * ***
*** * **** ** * * ** * * **** * *** ** ** * ****** * * * ** * * *** * * ** * * ***** * ***
* * **** ** ** ** * *** * * * ** ***** * * * **** * * ** * ** * *
            }
        }
    }

    return 0;

}
answered by (-249 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>

int main()

{
    int n,m,i,flag,count=1;
    scanf("%d %d",&n,&m);

    if (n<m)
    {
        for (n=n+1;n<m;n++)
        {
** **** * * * * **** * * * * * * * * * * ** * ********* *** * * * (i=2;i<=n;i++)
******** * * * * ** *** * **** ** * * *** * * * ** * * *
** ******** **** **** ** * **** *** * ** **** * ******** * ***** ** ** *** * * * * *
* **** ***** * ** * * *** ** * * * * * * * *** ** * * ** ******* ** * * **** *** ** ** ***** ** * (flag==0 && n==i)
* ** * ***** ***** * ** * **** ** ****** * * * ** ** * ** ***** * ****** ***** * ****
* **** * * * ***** **** ** *** * ***** * * ******* *** * * *** *** * ** * *** ** * * ** ** ** *** * ** (count==1)
** *** ** * * ** ** * ** **** ** * * * ** * ** * * ************* * * ** ** * *** ** ** * * ***** * ** * * ***
* * * * * * * * * *** ****** * ** * * * * *** * * ** * ** ***** *** * * * * *** * ** * ** * ** * ** * ** * ******* *
* ** * ** **** *** ** ** * * *** **** * * ** ** ****** ****** * * **** * * ** * *** ** ** ** ****** ** ** *** ** *** ****** * **
* ** * * **** * ** *** * **** * * * ** ** ** ** * *** * * * ** ** ** ** * ** * *** *** ** **
** **** ** * *** * ** **** **** * ** * * * * * **** ** * ** ****** **** *** * * * *** * * * ** * *
* ****** * ** *** ** ** ** ** * ** *** * * ** * *** * *** ** * ** * *** **** * * * * * ** * ** ** * ** **
* ** * * * ** **** * *** **** **** * ** **** ** * ** ** ** * ***** * *** ** **** * * * ** * * *** *** ** ** **** * ** *** ** %d",n);
* * ** * * * ** ** * * ** * **** * ** * ** **** * * ***** *** ** *** * ** *** ** ****** ***** * * * ** * * * ** * ******
*** ** ** ** ** ** * ** **** *** * * * * * * ** * ****** ** * * ** * ** ** ** * ** ** * * ** ** * **
* * * * * * ****** * * * ** * * ** ** ** * **** * ** * **** ****** * ** * * ******** ***
* * ** * * ** ** * * *** * ******* ** * ** ********** ** * * ** * * (flag==0 && n!=i)
* * **** * * * * ** * *** ** * * *** ****** * * *** ** **** * ** ** * ** * * *** ***
* * * ** ** * * * *** **** * **** **** **** ** ***** * ** **** *** ** *** * * *** *** *** ** **** *
* * ** ** * * *** * ** *** ** ** * *** *** ****** ** ** ** **** * * * * * * *** *** * *** ** **
*** **** * **** * ***** * ** * * ***** *** * * * *** ** *** *** *** * ***** * ** **
            }
        }
    }
    return 0;

}
answered by (-249 points)
0 0
prog.c: In function 'main':
prog.c:33:17: error: expected ';' before '}' token
                 }
                 ^
0 like 0 dislike
Hidden content!
#include <stdio.h>

int main(void) {
int a,b,n;
int first=0;
scanf("%d %d",&a,&b);
for (n=(a+1);n<b;n++){
***** *** ** ** **** ** * * d;
****** ****** ** * * (d=2;d<n;d++){
*** * * ** * ** * ** * *** * ** *** * ** * (n%d==0)
* ***** *** ** ******* * ** * * * ** *** * **
    }
*** ** **** * * *** * (d<n)
*** ** ****** * *** * **** ** ********** * *
* *** * * ** **** * **
*** * * ** *** ** * **** ** * ****** ** * ** * * (first==0)
*** * ** * ** * * * * *** **** *** * ***** * * * * **
* **** *** *** ***** * * * * * ** ** *** *
* *** * **** * **** * * * * *** * * ** ** * **** *** * * * **** ** * *** * * * ");
* * * * * **** * * ** * **** * * * * * * ** * *
* * ** *** ** *** * ********* * * *** * * **
** * ******** *** * **** **** * ** *** **** * *
*** * * *** * **
* ** ** ** ***** * * * ** * * 0;
}
answered by (-249 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>

int main(void) {
int a,b,n;
int first=0;
scanf("%d %d",&a,&b);
for (n=(a+1);n<b;n++){
* ** * * * ** * ** **** d;
* ** **** * * ** (d=2;d<n;d++){
* *** **** ** * ** *** *** * ** * ** ** **** (n%d==0)
** * *** * **** ** ** ** * * ** ** ** * **** **
    }
* * ** *** ** **** ** (d<n)
** * * *** ** *** * ** *** * ***** **
* ** *** *** * * * *****
**** *** * ** **** *** * * * * *** ** * **** * **** * * (first==0)
**** ** ** ** * * * ** **** * ******* * *** **** * ** ** * * ** **
*** * * * ** ** * * ** ***** ** * * * ***** * *** *
* * * ** * * **** ** **** * ** * *** *** * * * ******* * * * * ** * *
* *** ** ** ******* ** * ** * **** **
* ** * * *** * **** * * * * * * * *** * * * *****
****** ** * * *** ***** * *****
* * * ** * * ** ***
* * *** ***** * 0;
}
answered by (-249 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>
int *** * ** ******* *** *** ***** *** ** ****

int Is_the_number_prime(int num){

**** * *** *** * *** ** * fac;
** * * *** **** **** flag=1; ///is prime by default

* *** ****** * ** ** *** * * * * * *
* ******* ** * * **** ** * *** * * ** * * * * ///sets flag for not prime
* **** * * * * ***** *


** * ** * ** *** *** ** * * * ***
* ** ** * * *** *** ** * ****** **** *** flag;
* * ** *** * ** ** **** return 0;
}

int Is_the_next_number_prime(int num,int limit){

* *** *** * fac;
* * **** * * * * * * ** * flag=1; ///is prime by default
** ** * ** *** = num+1;
* * ** ** * * ** * **** *** * ****
* * *** * * * * ** **** * **** * **** **** * ** * * * * * ///sets flag for not prime
* ** ** * * * * * *** * * **


*** * **** **** **** **** * * && num<limit){
**** * ** ** * * * ** ***** **** **** * * flag;
** * * *** *** **** ** ** return 0;
}

int main()
{
* * * * ** **** * *
* * * ** * ****** **** * * %d",&n, &m);
* *** * *** ** ** * if the number is prime
* * ** * ** * * * ** *** ** ** * **
****** * ** * **** * ** ** * ** * * ** * *
* * **** ** ** ** *** ** * * **** ** * * ** * * **** * * * ** *** *** * **** *
* **** ** * ** ***** * ** * * ** * *** * * ** *** * **** * * ** = 1;
**** ** ** * * ** ** * ** *** ****** *
** ** ** * ** * * *** * **** * ** *** ** * * * *** * * ** && primealreadyprinted == 1){
* * ** * * **** * ** * * ****** * ******* ** *** * * ** * * ");
* ***** ** * * **** * * ** **** *** *** * ****
** * ** *** * * **
** ** * * ** * * * 0;
}
answered by (-286 points)
0 0
Case 0: Wrong output
Case 1: Correct output
Case 2: Wrong output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>
int ** ** * ** *** **** * * *** * * * ** * ** **

int Is_the_number_prime(int num){

**** **** * ** * * ** fac;
* * *** ** ** ** * flag=1; ///is prime by default

* * * ** * * * * **** ***** ** ** ** **
* *** *** *** *** *** * * * * ** * * * ** ** ///sets flag for not prime
** ** * * * **


**** * ** * ** ** * *** * * ***
* *** ** ** * * ** *** *** * * * * ** ** ** * *** flag;
** ****** * * return 0;
}

int Is_the_next_number_prime(int num,int limit){

* * * ** ***** *** fac;
* * * ** * * ******* flag=1; ///is prime by default
* *** * * * * * * *** * = num+1;
* * * ** * * *** * * *** * * *** *
** **** **** ** ** * * ** ***** * * ** ***** *** * ///sets flag for not prime
** * * ** * * * * **


** *** * * * * * * * * *** * && num<limit){
* * ** ** * * ******* **** ***** * ** ** flag;
* * *** *** * *** * * return 0;
}

int main()
{
** * * * ******* * * ***
*** * * * * * * * * * ** * %d",&n, &m);
* ** *** *** ****** * **** *** if the number is prime
* ****** *** ** ******* ******* ** ***** *
* * * * ** * * * *** * ** ** ** * * *** ** * * * * * *** *
*** * ****** ** * * * *** *** ** * * ****** ** * * * * ***** **** ** *** *
***** * * *** * * *** **** *** * * ********* * ***** * **** *** ** ** ** * = 1;
* ** * **** * ** * ** *** * * *** **** * *
* * *** *** ******* * * * * * * *** * *** *** * ***** ** ** ** && primealreadyprinted == 1){
* * * ** *** ** ** * * ** *** * **** ** * ********** * ** * * * * **
* * * * * *** ** ***** ****** *** **
** * *** * *** * *
** * * ** * * ** * ** * 0;
}
answered by (-286 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>
int ** ** *** * * **** * ** * ** *** ** *

int Is_the_number_prime(int num){

* ** ***** * ** ** * **** fac;
* * ** *** * * ** ** **** flag=1; ///is prime by default

* * ***** ** * ***** * * **** * ***** *
* ******* ****** ** ******* * * ** * **** *** * *** ** *** ** * * *** ///sets flag for not prime
* * * * * * * *** **


** * * **** * **** * ******* * ** ** *
* ** * * ** * ** *** **** * * ** * * ** * flag;
** ** * *** * ** *** ** * * * * return 0;
}

int Is_the_next_number_prime(int num,int limit){

* ** * * ** * * ** * fac;
** *** * ** ** * ** ** flag=1; ///is prime by default
*** * * * * *** *** * ** * = num+1;
* *** **** ** ** *********** ***********
* * * ** ** * ** * ** ** ** * * * * ** ** ***** ///sets flag for not prime
* ** * * ** * ***** *


*** * ** * ** *** * * ***** && num<limit){
* *** * ** ** ****** * **** ***** * * * flag;
* * * ** * * * **** return 0;
}

int main()
{
*** ** *** *** ** ** * **
* * ***** ** * * * * ** ** %d",&n, &m);
** *** * ** * * * ******* if the number is prime
* * *** ** ** *** * ** * **
**** * * ** ** * ** * * * ** *** * *** **
**** ** * ** ** ** ** *** * *** * * *** ** ** * * *** ** *** * * * ** * *
* ** ** *** * * ** ** ** * * * * * *
* * * *** * **** * **** * ** ** *** * * * ** * **** ** * **
* ** ** ** * * * ** ***** *** ** * **** *** * * * * ** * * * ** * ");
** ** * * *** ** * * *** * ** ** *

* **** * ** ** *







** **** ** * * * * 0;
}
answered by (-286 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>
int * ** ** * ** ** ** * *** ** ** ** * *

int Is_the_number_prime(int num){

*** ** * ** * **** ** fac;
*** *** ********* ** flag=1; ///is prime by default

** * ** **** *** * * * * * *** * ***** *
***** * ***** ***** ** **** * * ****** ******* * * * ///sets flag for not prime
** ** * * ** **


*** ** * **** *** *** * * *
** ** ** ****** *** * * * * ***** ***** ** flag;
* * **** ** * * * return 0;
}

int Is_the_next_number_prime(int num,int limit){

** * ** *** *** * * fac;
* * ** **** * *** flag=1; ///is prime by default
** ** ** *** ** * * * = num+1;
* ** * * * * ** * *** * * ** * * *** *
****** ** * ** ***** * * * * * ** ******* ** *** **** **** ///sets flag for not prime
**** ** **** ** * ** **


* *** * * **** * ** ****** * ** && num<limit){
** * **** * * * * ** ** * * *** * * ** flag;
** ****** * * **** ** * return 0;
}

int main()
{
*** ** ***** *** **** * * **
* ** ** * **** * ** * ****** %d",&n, &m);
* * * ****** * ** * * ** * * if the number is prime
* ***** * * ** * * * * **** *
** ** * * *** * * *** * * **** * ** ** * ** ** ** * ***
* * *** ** ******* ** *** *** ** * * * * * *** * * ** * * *** * ******* *** **** *** *
**** * * ** ** * * * ** ***** **** *******
******* ** * * *** * * **** ** * * ** * ** * ** * ******* *** *** ***
* * * ** * * * * * * * * ** * *** ****** ** * ** * * ** ** ** *
*** * * * * * **** ***** *** * * **** *

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







* ** ** * ** ****** ** 0;
}
answered by (-286 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>
int n,m,i,ii,notprime=0;
int main()
{
*** * * ** **** **** * * *
* **** *** * ** **** ** *** %d",&n, &m);
** ** ** * * * * * **** *** if the number is prime
    for (i=n;i<m;i++){

*** ***** * *** **** * * ** * **** * ** ** * ** * ** * *
** ** **** * ****** * ****** *** ** *** ** * * * ** ** ** ** * * ///if the number is prime
* **** ** *** * * * ** **** ** *** *** * ***** ** * * * * ** * *** * * * ** * * * * * *** * * ***** * the next number
* * * *** *** * ***** *** ***** ******* * * * ***** * **** * **** ** *** * *    ///reset the factors to 2 for the next test
** * * ** *** * * ** *** * * ** **** * * * * * * ** * ** ***
** **** * * **** * ****** * *** *
* *** * * ** * * ** ** *** * * ** * ** * ****** **** ** ** * * **** *** **** ** * **
* ***** * * ***** * *** * ** * ** * * *** ** * ** ** ** * ** ** ** i); ///if the number passed the test, it gets printed
* * * * * *** ******* * * * * ** * *


* * * * ** * ** ****** *** * *** * *** * ** * * ///checks if next number is prime
****** * *** ***** * * * * * ** * **** * * * * * * *** ** * *** * * * * *** **** * * * it is not prime
** * ** *** ******* ** ** ** * * ** * ** ** * ** * * ****** **** * *** ***** *** * * * *    ///flags is as not prime
** * ** * ** ** ****** * ** * * ** * *** * * * **

* * * ***** * *** ** * ** ** ** * * * * * *
***** ** ** *** * ** **** * * ***** *** ** *** * * *** * ** * ** * * * * * ** ** * * **** ** * * ///if flag says it is prime and the next number is less than m
* ** *** ***** * * **** * * * * *** *** * * * ********** * * *** "); * ******** * * **** ** * * * * ** *** * space for next prime

** ** *** * * * * *** * ** ***** ** *******
** ** * * ** * *** ***** *** *** ** * *** * * * * * * ** *** * ** **** **** ** * *** * ** ** ** * * *** * * * ***** *** * ***** flag
    }



* ** ** * * ***** * * * 0;
}
answered by (-286 points)
0 0
Case 0: Wrong output
Case 1: Correct output
Case 2: Correct output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>
int n,m,i,ii,notprime=0;
int main()
{
* *** * *** *** * *** *
*** * ** *** ** * * * *** * *** %d",&n, &m);
* **** ** *** *** * * if the number is prime
* * * * ****** * (i=n;i<m;i++){

* ***** * ** ** * *** ** ***** *** * ** ******* ** *
* * * * *** * ** ** ** *** * * ** * * * * * ** * * * **** *** **** ///if the number is prime
* *** *** * *** **** ****** ***** * * ****** **** ****** ** * ** * **** ** * * * * * * ** ** ** * ***** ***** the next number
* * * **** * * * ****** * * * *** ****** * *** * ** * ** * * *** ** * ** *    ///reset the factors to 2 for the next test
** * * * * * * *** * **** **** ** * ** * ***** * * *
** *** ***** *** *** * * * * * * * **
*** ** * * ** * * *** *** * * ** *** *** * ** ** ** * * * ** * * ****
* * ** * * * **** * * * * ** * ** ** * * * **** * * * ***** ** ** ***** ** * i); ///if the number passed the test, it gets printed
** *** * *** * **** ** * **** ** * **


* * * ** * ** ** * * ** *** ***** * *** ** ** * ***** ** ///checks if next number is prime
** *** ** * * * * * **** ** * * *** * ** ** *** * * * * * * *** ** *** * *** ******* ** * * *** it is not prime
****** *** * * *** * * * * * ** * * * ** * *** ** ** *** * * ***** * ** * **** * ** ****    ///flags is as not prime
* ***** *** *** * ** ** ****** ** * ** * *** * * **

* * ***** *** * * * * *** * * * *
* * * * * ** * * *** * * *** * ** * * **** * ** *** * ** ** ** * *** ******* *** ** * ///if flag says it is prime and the next number is less than m
** * * * * * * * *** ********* * * * * * ** * * * ** *** ** ** "); * ** *** *** ** *** * * ****** * * * ** space for next prime

* *** * * * ** * * * ** ** ****
* ** ** ** * *** *** ** ** ****** * * * ** ** ** * *** * * * ** ** * * * * * * *** **** * * * *** flag
    }



* * *** * * ** ****** * 0;
}
answered by (-286 points)
0 0
Case 0: Wrong output
Case 1: Correct output
Case 2: Correct output
Case 3: Wrong output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:162.159.115.10
©2016-2026

Related questions

0 like 0 dislike
72 answers
[Exercise] Coding (C) - asked Nov 2, 2017 in Chapter 6: Loops by semicolon (5.2k points)
ID: 29831 - Available when: 2017-11-02 18:00 - Due to: Unlimited
| 18.1k views
0 like 0 dislike
93 answers
[Exercise] Coding (C) - asked Nov 2, 2017 in Chapter 6: Loops by semicolon (5.2k points)
ID: 29830 - Available when: 2017-11-02 18:00 - Due to: Unlimited
| 22.8k views
12,783 questions
183,442 answers
172,219 comments
4,824 users