Computing.Net > Forums > Programming > Prime number checking

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Prime number checking

Reply to Message Icon

Name: Farmer John
Date: April 12, 2003 at 00:59:58 Pacific
OS: Win XP
CPU/Ram: AMD1800/256ram
Comment:

i am tryimg to write a java program to check input no.to se if it is a prime no.
I have to request a positive no. input and then loop through the formula(i=2;i*i=n;i++)
n=input no.Any assistance welcome



Sponsored Link
Ads by Google

Response Number 1
Name: Alam
Date: April 12, 2003 at 11:04:21 Pacific
Reply:

 /* Code in C for Prime Number */

main()
{
   int TRUE=1,FALSE=0,PRIME,n,i;

   clrscr();  /* Clear the screen */

   /* Prompt user to Enter a number */
   printf("Enter the no. ");

   /* Accept the number in variable n */
   scanf("%d",&n);

   /* Assume the no. to be Prime */
   PRIME=TRUE;

   /* Run the loop from i=2 till
   square of i = n */
   for(i=2;i*i<=n;i++)
   {
      /* if for any value
      n is divisible by i
      i.e. remainder is 0 */

      if(n%i==0)
      { PRIME=FALSE;
     break;
     /* then change the value of PRIME
     and end the loop */

      }
   }

   /*check the value of PRIME
   and display appropriate message*/

   if(PRIME==TRUE)
     printf("The no. is prime");
   else
     printf("The no. is not prime");

 }


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Prime number checking

Find prime numbers in Java? www.computing.net/answers/programming/find-prime-numbers-in-java/11287.html

prime numbers execution problem www.computing.net/answers/programming/prime-numbers-execution-problem-/13104.html

Prime Number in MIPS help www.computing.net/answers/programming/prime-number-in-mips-help/13454.html