Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Tuesday 6 November 2018

List of all Primes up to a given number

#include <stdio.h>
int *getPrimes(int n) {

   static int  r[10];

 int i,j;
    
 int count = 0, s=0;
    
    for(i = 2 ; i <= n-1 ; i++)
    {
        for(j = 1 ; j <= i ; j++)
        {
            if((i%j)==0)
            {
                count++;
            }
        }
        if(count == 2) 
        {
            r[s] = i;
            s++;
        }
        count = 0;
    }

return r;    
}


int main () {

   int *p;
   int i;
  // int n=100;
   int count = 0;
   
   printf("enter number");
   scanf("%d",&n);
   p = getPrimes(n);

   for ( i = 0; i <n ; i++ )
   {
      count++;
     printf( "%d\n",p[i]);
      if(p[i] == 0)
         {
             break;
         }
     }
 return 0;
}

1 comment:

  1. Thank You, for such useful information.
    Your posts are always easy to understand and Helpful.

    ReplyDelete