Tuesday, July 28, 2009

How to make a program of displaying prime numbers in c programming?

upto 100 and using for loop

How to make a program of displaying prime numbers in c programming?
THIS IS IN C++/IT IS SIMILAR IN C but THERE IS NO CLASS IN C:





SOURCE CODE:





#include %26lt;iostream.h%26gt;





class prime


{


int prime_cnt;


int prime_number;


public :


// constructor


prime ( int num_in = 1, int prime_in = 2 )


// set default values


{


prime_cnt = num_in;


prime_number = prime_in;


}


// get the next prime number by overloading the ++ operator


prime%26amp; operator++();





// accessor member functions


int get_nth_prime()


{


return ( prime_cnt );


}





int get_prime()


{


return ( prime_number );


}


};


//------------------------------------...


// overload the output %26lt;%26lt; operator of stream i/o to print prime number


// use a reference to an ostream and this class 'prime' and return an


// ostream reference


//------------------------------------...


ostream%26amp; operator %26lt;%26lt; (ostream%26amp; strm_ref, prime %26amp;prm_in)


{


return strm_ref %26lt;%26lt; prm_in.get_prime();


}





//------------------------------------...


// here where we create an new prime number upon invocation


// the overloaded ++ operator returns a new prime number


// To the user of the class it is as easy as adding one to integer.


//------------------------------------...


prime%26amp; prime::operator++()


{


++prime_cnt; // how many prime number so far?


// test for 2


if ( prime_number == 2 )


{


++prime_number;


return (*this);


}


for( ; ; )


{


prime_number += 2;


int prime_found = 1;


int max_times = ( prime_number / 2 );


for ( int i = 3; i %26lt; max_times; ++i )


{


if ( ( ( prime_number / i ) * i ) == prime_number )


{


prime_found = 0;


break;


}


}


if ( prime_found )


break;


}


return ( *this );


}


//------------------------------------...


// Now to use this class is as easy as defining another int in c


//------------------------------------...


int main ()


{


const int no_of_primes = 100;


//------------------------------


// here is our prime class


//------------------------------


prime prm_num;


cout %26lt;%26lt; "generate " %26lt;%26lt; no_of_primes %26lt;%26lt; " prime numbers " %26lt;%26lt; "\n";


while ( prm_num.get_nth_prime() %26lt;= no_of_primes )


{


cout %26lt;%26lt; "prime number " %26lt;%26lt; prm_num.get_nth_prime()


%26lt;%26lt; "\t" %26lt;%26lt; prm_num %26lt;%26lt; "\n";


// get the next prime with the overloaded ++ operator


++prm_num;


}


cout %26lt;%26lt; " end of program " %26lt;%26lt; "\n";


return (0);


}
Reply:Just curious... Is this a homework project.. lol :)
Reply:#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;stdlib.h%26gt;


#include%26lt;math.h%26gt;


int i,j,k,l,m;


int c[100];


void main()


{


int ch;


int a[300];


clrscr();


printf("\n Enter the value of n");


scanf("%d",%26amp;n);


for(p=2;p%26lt;=n;p++)


{


a[p]=p;


for(p=2;p%26lt;sqrt(n);p++)


{


if(a[p]!=0)


{


j=p*p;


while(j%26lt;=n)


{


a[j]=0;


j=j+p;


}


}


}


m=0;


printf("\n The prime nos:");


for(k=0;k%26lt;n;k++)


{


if(a[k]!=0)


{


c[m]=a[k];


m++;


}


}


for(k=0;k%26lt;m;k++)


{


printf("\n %d",c[m]);


}


getch();


}


No comments:

Post a Comment