1 Replies - 5220 Views - Last Post: 24 April 2007 - 10:24 AM Rate Topic: -----

#1 ATXFG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-April 07

First 50 Prime Numbers

Posted 24 April 2007 - 09:35 AM

This code determines whether, it is a prime number or not, 1 for true, 0 for false, how would I print out the first 50 prime numbers. Start with a while loop, probably.

#include<stdio.h>
#include<conio.h>
#include<assert.h>

int is_prime(int n);

void main(void)
{
	int n=0;
	clrscr();
	printf("An integer ");
	scanf("%d",&n);
	assert(n > 1);
	n=is_prime(n);
	if (n==1)
		printf("\nThe number is prime");
	else
		printf("\nThe number is NOT a prime");
	getch();
}

int is_prime(int n)
{
	int i;
	for(i=2;i<n;i++){
		  if (n%i)	 continue;
		  else	 return 0;
	}
	return 1;
}


This post has been edited by ATXFG: 24 April 2007 - 09:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: First 50 Prime Numbers

#2 vasdueva  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 03-April 07

Re: First 50 Prime Numbers

Posted 24 April 2007 - 10:24 AM

You could add a while loop yes.

while(numberOfPrimes <50)
{
//test for primes
numberToBeTested++;
if(primeFound)
{
numberOfPrimes++;
}
}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1