Sunday, August 2, 2009

How 2 write a turbo C program 2 find prime numbers?

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


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


main()


{


int i,n,j,k=0;


printf("Enter the number.......");


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


for(i=1;i%26lt;n;i++)


{


j=n%i;


if(j==0)


k=k+1;


}


if(k==1)


printf("yes its a prime number");


else


printf("u fool check the number, its composite");


}

How 2 write a turbo C program 2 find prime numbers?
Here's an example:


http://finiteonline.tripod.com/cprogs.ht...





Alternately, write a Forth interpreter in C and load this program of mine into memory:


: log2/? ." Gives the base 2 logarithm, rounded down. Returns 0 for values less than 1." cr ;





: sqr-guess dup log2 2 + dup * / 1+ ;





: sqr-guess/? ." Estimates the square root, badly. Ideal for numbers around 10,000 to 100,000." cr ." Primarily used as a starting point for the iterative square root calculator." cr ;





: sqr dup sqr-guess


begin


dup dup dup * swap 1+ dup * 3 pick %26gt; -rot 3 pick %26lt;=


tuck not if 1- then rot tuck not if 1+ then -rot and


until


nip


;





: sqr/? ." Gives the square root of the number, rounded down. Uses an iterative method" cr ." based on a starting point guess." cr ;





: isprime dup 4 %26lt; if 1 %26gt;


else dup sqr


begin


2dup / over * rot dup rot = not negate rot *


1- dup 2 %26lt;


until


nip 0%26gt; then


;





: isprime/? ." Figures if a given number is prime, using the sqr function." ;





: primelist 2


begin


dup isprime if dup . then 1+ 2dup %26lt;


until 2drop ;

innia

No comments:

Post a Comment