Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why does it not print prime number till n

–2 votes
asked Jun 10, 2018 by anonymous
// Sample code to perform I/O:

#include <stdio.h>

int main(){

int num,i,j,k,c=0;

scanf("%d", &num);              // Reading input from STDIN

     // Writing output to STDOUT

     for(j=2;j<num;j++)

     {

     for(i=2;i<j;i++)

   {

       if(j%i==0)

       c++;

   }

   if(c==0)

   printf("%d ",j);

     }

}

2 Answers

0 votes
answered Jun 11, 2018 by KAUSTAV (720 points)

See, in your code you have not written printf to take input of "n" from the user. I have written the correct code below with printf in bolded form.

// Sample code to perform I/O:

#include <stdio.h>

int main(){

int num,i,j,k,c=0;

printf("Enter n value = ");

scanf("%d", &num);              // Reading input from STDIN

     // Writing output to STDOUT

     for(j=2;j<num;j++)

     {

     for(i=2;i<j;i++)

   {

       if(j%i==0)

       c++;

   }

   if(c==0)

   printf("%d ",j);

     }

}

0 votes
answered Jun 19, 2018 by Alaris
#include <stdio.h>

int main ()
{
  int num, i, j, k, c = 0;
  printf ("Enter n value = ");
  scanf ("%d", &num);        // Reading input from STDIN
  // Writing output to STDOUT
    for (j = 2; j < num; j++)
    {
        c = 0; // you forgot to reset c
        for (i = 2; i < j; i++)
        {
            if (j % i == 0)
            c++;
        }
        if (c == 0)
            printf ("%d ", j);
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...