Computer Science, asked by srujanachooc, 1 year ago

print alternate prime numbers

Answers

Answered by Atulkrishnan
0
Hai,I will just tell you algorithms for it.There are two ways to do this.Either by using the Sieve of Eranthuthesis or by a normal method.I belive that you know what is seive of eranthuthesis.If not just search for it on the internet.Its a method to find the prime numbers from n natural numbers.Then after finding the numbers assign them to an array of integers and then by any loop(for loop is reccomended)just print them alternately.Another way is by checking each digit by dividing it with numbers starting from 2.I will write the code for this as it is short:

                   int a,b,n;
                    int A[200];
                    int i=1;
                    cout<<"ENter upto what value:";
                     cin>>n
                    A[0]=2
                   for(a=2;a<n;a++)
                        { for(b=2;b<=a/2;++b)
                          {   if(a%b==0)
                                  break;
                              else
                              {   A[i]=a
                                i++;
                              }
                          }
                        }
                       for(i=0;i<n;i=i+2)
                       {   cout<<A[i]<<endl;
                       }   

Atulkrishnan: PLZ DO CHECK THE CODE FOR ANY BUG BY COMPILING IT AS I AM NOW USING A SMARTPHONE
Atulkrishnan: also in the second loop I have used the pre increment...plz if you find any problem with that change that to post
Similar questions