Question #10950248

Fortran 90: Develop a Code to Detect Prime Numbers?

Write a Fortran function called IsPrime(N) which determines whether or not its integer argument is a prime number. The function should be of type LOGICAL. A prime number has no integer divisors other than 1 and itself. Of course, dividing numbers up to √N is sufficient. Use this function in a program to find all prime numbers between 1000 and1050. This is what I have so far: Program main !----------------------------------------------------------------------- ! A Program which determines whether or not its integer argument is a ! prime number. ! ! Input : ! ! n : number ! ! Output: ! ! !----------------------------------------------------------------------- Implicit None Logical::isPrime, x Integer::n, i, w Interface Function isPrime(n) Integer,intent(in)::n Logical::isPrime Integer::m End function isPrime(n) End Interface Do Write (*,*)"Enter a positive number to see if it is a prime" Write (*,*)"Or enter 0 to quit" Read (*,*)n If(n==0)Then Exit End If If(n<0)then n=-n End If If(isPrime(n))Then Write(*,*) n, "is a prime" Else Write(*,*) n, "is not a prime" End if End do Do i = 1000,1050 x = isPrime(i) Write (*,10) "the number", i, "is prime", x 10 Format (A,I5,A,L2) End Do End Program main function isPrime(n) Implicit None Integer,Intent(In)::n Logical::isPrime Integer::m isPrime=.false. If(n<2)then Return End if Do m=2,int(sqrt(real(n))) If(mod(n,m)==0)then return End if End do isPrime=.true. end function isPrime(n)

2013-10-09 02:57:58

TELL US , if you have any answer

There is NEVER a problem, ONLY a challange!

The helpinganswers.com is a free-to-use knowledgebase.
  The helpinganswers.com was started on: 02.07.2010.
  It's free to register. Once you are a registered user, you can ask questions, or answer them.
  (Unless registration you can just answer the questions anonymously)
  Only english!!! Questions and answers in other languages will be deleted!!

Cheers: the PixelFighters

  Contact: support@helpinganswers.com

C'mon... follow us!

Made by, history, ect.