Click here to Skip to main content
15,446,510 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this code is for a program that prints the prime numbers between two specific us and characters must be printed , my does not print character ,

What I have tried:

C
#include <stdio.h>;

int main()
{
    int i, j, start, end;
    int isPrime;
  
    scanf("%d", &start);
    
    scanf("%d", &end);

    if(start < 2) 
        start = 2;
  
    for(i=start; i<=end; i++)
    {
        isPrime = 1; 
       
        for(j=2; j<=i/2; j++)
        {
            if(i%j==0)
            {
                isPrime = 0;
                break;
            }
        }
		
        if(isPrime==1 )
        {
            printf("%d,", i);
        }
    }

    return 0;
}
Posted
Updated 25-Apr-21 4:30am
v2
Comments
Patrice T 25-Apr-21 8:38am     CRLF
In html, '<' have a special meaning and need to be encoded to avoid problems. Use Improve question to update your question.
Richard MacCutchan 25-Apr-21 8:49am     CRLF
When I run your code I get the following result:
C:\Users\rjmac\Documents\VSCode\C++>cp1172,3,5,7,11,13,17,
What is the problem? Also, please edit your question to remove those spurious characters.

Quote:
How to print a character , between output numbers

Can you give more details ?
Because actually, your code just print a comma between primes.
 
Share this answer
 
Instead of always printing like this:
C
printf("%d,", i);
you need to be a little cleverer - ort you list will always end with a comma:
2, 3, 5, 7, 11, 13, 17,

So ... set a variable before the loop to zero and check it in the loop:
C
if (isPrime)
   {
   if (isNotFirst)
      {
      printf(",");
      }
   printf("%d", i);
   isNotFirst = 1;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900