VB icon

lcpri.c

Email
Submitted on: 1/1/2015 8:14:00 AM
By: Luke (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: C
Views: 882
 
     How many prime numbers are there between 1 and X? You just have to choose X and this program will do the rest. Click Luke to go to my Web Page: other source code available...

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: lcpri.c
// Description:How many prime numbers are there between 1 and X? You just have to choose X and this program will do the rest. Click Luke to go to my Web Page: other source code available...
// By: Luke (from psc cd)
//
// Inputs:an integer number
//
// Returns:the number of prime numbers lower than X
//**************************************

/*
 Save this file as lcpri.c 
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
 ldiv_t dividi;
 int n, totprimi, primo;
 int cont1, cont2;
 printf("\n\tEnter an integer (>= 1): ");
 scanf("%d", &n);
 if (n < 1)
 {
 printf("\n\t...I said >= 1...bye!\n");
 return(-1);
 }
 totprimi = 0;
 for(cont1 = 1; cont1 < n; cont1++)
 {
 primo = 1;
 for(cont2 = 2; cont2 <= (cont1 / 2); cont2++)
 {
dividi = ldiv(cont1, cont2);
if (dividi.rem == 0)
{
primo = 0;
break;
}
 }
 if (primo) ++totprimi;
 }
 printf("\n\tThere are %d prime numbers lower than %d\n", totprimi, n);
 return 0;
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.