Tuesday 16 May 2017

PRINTING prime numbers in a given range by pointers in c++


#include<iostream>
using namespace std;
int primes(int * a,int start,int end);
int main(){
int start,end;
int arr[100]={0};                               //array initialized with zero
cout<<" enter the starting value "<<endl;
cin>>start;                                       // start point of array
cout<<endl<<" enter the end point "<<endl;
cin>>end;                                            // end point of array
int a=primes(arr,start,end);                //calling prime function
cout<<" so the prime numbers are "<<endl;
cout<<a<<endl;
for(int x=0;x<a;x++){
cout<<x<<"  =  "<<arr[x]<<" "<<endl;
}
}
int primes(int * a,int start,int end){
int iter=0;                                    //will count total prime numbers
for(int x=start;x<=end;x++){

int count=10;                          // will become negative if number is not prime
for(int y=2;y<=(x/2);y++){
if(x%y==0){                      // if condition is true number is not prime
count=-5;

}
}
if(count>0){
*a++=x;
iter++;
}
}

return iter;                            // total number of prime numbers
}




































No comments:

Post a Comment