Please, I need help in this little problem of printing all the prime numbers from to 100. Please, if you have more than one method to the question, I would really appreciate it. I also need it as soon as possible. Thanks.

Recommended Answers

All 4 Replies

Well, you can start off with the first two: 2, and 3, and store them in an array. From there, think of the conditions for a number to be prime and implement it, keeping in mind to compare each successive value against the prime array.

Here's hoping it helps.

#include <math.h>
#include <stdio.h>

int main(void){
	int i, j, isPrime;
	for(i = 2 ; i <= 100 ; i++){
		isPrime  = 1;
		for(j = 2 ; j <= i ; j++) {
			if( i == j)
				continue;
			else if( i % j == 0)
				isPrime = 0;
		}
		if(isPrime)
			printf("%d, ", i);
	}
	printf("\n");
	return 0;
}

hope this helps...

commented: This is a disservice -1

#include<stdio.h>
main()
{
int i,j,flag=0;
for(i=1:i<=100;i++)
{
flag=0;
for(j=2;j<=(i/2);j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==1)
printf("%d\t",i);
}
}

commented: Incorrect. Learn how to post code first. -1

And here I thought the point wasn't to simply give the answer.

commented: Yes, it is. +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.