Make your own free website on Tripod.com
 

C PROGRAM:

Determining Prime Numbers

 

Home
Up
About Us
Discussions
Quizzes
Reflections
Flowcharts
Feedback

 

Here is a sample Turbo C program that determines whether a number given by the user is prime or composite. For expert programmers, you may need to do some adjustments if you are using another language apart from C. For those who do not understand programming, just click on the link at the bottom of this page and try the finished program out for yourself.

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#define p printf
#define s scanf
int ctr,x,n,nmod;
char cont;
main()
{

do
{  clrscr();
   ctr=0;
   p ("Input a number: ");
   s ("%d", &n);
   if (n<0) p ("\nINVALID NUMBER");
   else if (n==0||n==1)

       p ("\nNEITHER PRIME NOR COMPOSITE");
   else if (n>1)
   {   for (x=1; x<=n; x++)
       {   nmod=n%x;
           if (nmod==0) ctr+=1;
       }
   if (ctr==2) p ("\nPRIME");
   else p ("\nCOMPOSITE");
   }
   p ("\n\nDo you wish to continue [Y/N]? ");
   cont=getche();
}
while (cont=='Y'||cont=='y');
}

 


CLICK TO SEE ACTUAL PROGRAM (.zip)

Home | C PROGRAM: Determining Prime Numbers | C PROGRAM: Prime Factorization | C PROGRAM: Divisors

 

Copyright 2006. All rights reserved.

No part of this site may be reproduced without the permission of the authors.