hi friends, im a newbie at C,
im trying to code for a program which cud return all prime numbers in a specific range.
its returning the results with an error, which is , it also includes the products of two prime numbers in results,
for example 15=3*5 and 21=3*7
my code is

#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
for (int i=1;i<=300;i++)
{
for(int j=2;j<=i;j++)
if(i%j==0 )
break;
else
{
printf("\t%d ",i);
break;
}
}
getche();
}

Result:
3 5 7 9 11 13 15 17 19 21 23 .................
kindly guide me where the problem resides.
which is letting 15,21 etc in output.
Help ..C program for finding Prime numbers ...issues

 Lance Naik
 Posts: 28
 Joined: Tue Sep 02, 2003 1:16 am
 Location: Karachi
 Contact:

 Site Admin
 Posts: 5132
 Joined: Fri May 02, 2003 10:24 am
 Location: Karachi
 Contact:
Re:
Farrukh Ahmed

 Lance Naik
 Posts: 28
 Joined: Tue Sep 02, 2003 1:16 am
 Location: Karachi
 Contact:
Correct Code
dear farrukh,
The code presented in the mentioned example is far far away from the understanding of even a regular C programmer.
extensive use of pointers and custom functions to compact the code is visible.
but anyhow, its appreciable that u tried to help in any sense.
i've fixed the issue with the program and its working perfect now.

void main()
{
for ( int i=1;i<=32000;i++) // range to find prime numbers is 132000
{
int p=1; // starting with 1, taking it as prime,(factors, 1 & 1)
for(int j=2;j<=(i/2);j++)
if(i%j==0) //if the result is 0, its not prime
{
p=0; // so , we should halt here, and start again with next
break;
}
if(p) // if its prime
{
printf("\t%d ",i); // print it here
}
}
}
The code presented in the mentioned example is far far away from the understanding of even a regular C programmer.
extensive use of pointers and custom functions to compact the code is visible.
but anyhow, its appreciable that u tried to help in any sense.
i've fixed the issue with the program and its working perfect now.

void main()
{
for ( int i=1;i<=32000;i++) // range to find prime numbers is 132000
{
int p=1; // starting with 1, taking it as prime,(factors, 1 & 1)
for(int j=2;j<=(i/2);j++)
if(i%j==0) //if the result is 0, its not prime
{
p=0; // so , we should halt here, and start again with next
break;
}
if(p) // if its prime
{
printf("\t%d ",i); // print it here
}
}
}
look at how the code works. that will help you understand the problem. here, use this:
output:
maybe he said it, maybe he didn't. show proof.
Code: Select all
#include<stdio.h>
int main()
{
int i;
int j;
for (i = 1; i <= 300; i++) {
for (j = 2; i <= i; j++) {
if (i % j == 0) {
break;
} else {
printf("i %d j %d i %% j %d\n", i, j, i % j);
break;
}
}
}
}
output:
Code: Select all
i 1 j 2 i % j 1
i 3 j 2 i % j 1
i 5 j 2 i % j 1
i 7 j 2 i % j 1
i 9 j 2 i % j 1
i 11 j 2 i % j 1
i 13 j 2 i % j 1
i 15 j 2 i % j 1
i 17 j 2 i % j 1
i 19 j 2 i % j 1
i 21 j 2 i % j 1
please provide a factual reference for the above quote from muslim sources, or stop using it in your signature. someone who studied the issue wrote this article.Burn the libraries, for thier price is only one book....AL QURAN (UMER FAROOQ r.a)
maybe he said it, maybe he didn't. show proof.
you posted your fixed code while i was writing my post. oh, well.
a good article that describes several techniques for finding primes is here.
a good article that describes several techniques for finding primes is here.

 Lance Naik
 Posts: 28
 Joined: Tue Sep 02, 2003 1:16 am
 Location: Karachi
 Contact:
Many thanx, Lambda
You were really helpful both with the program and the quote,
ist... the output presented was very easy for anyone to understand.
but anywayz i had figured that out earlier.
second..after reading above and few other articles i am dropping that quote off.
i read that in the Dictionary of Quotations, published by indians.
many thanx for correction
ist... the output presented was very easy for anyone to understand.
but anywayz i had figured that out earlier.
second..after reading above and few other articles i am dropping that quote off.
i read that in the Dictionary of Quotations, published by indians.
many thanx for correction
lambda wrote:look at how the code works. that will help you understand the problem. here, use this:Code: Select all
#include<stdio.h>
int main()
{
int i;
int j;
for (i = 1; i <= 300; i++) {
for (j = 2; i <= i; j++) {
if (i % j == 0) {
break;
} else {
printf("i %d j %d i %% j %d\n", i, j, i % j);
break;
}
}
}
}
output:Code: Select all
i 1 j 2 i % j 1
i 3 j 2 i % j 1
i 5 j 2 i % j 1
i 7 j 2 i % j 1
i 9 j 2 i % j 1
i 11 j 2 i % j 1
i 13 j 2 i % j 1
i 15 j 2 i % j 1
i 17 j 2 i % j 1
i 19 j 2 i % j 1
i 21 j 2 i % j 1please provide a factual reference for the above quote from muslim sources, or stop using it in your signature. someone who studied the issue wrote this article.Burn the libraries, for thier price is only one book....AL QURAN (UMER FAROOQ r.a)
maybe he said it, maybe he didn't. show proof.
Dear Lambda
Nice Program.
Regards
Noman Liaquat Khanzada Rajput
Linux means productivity and fun.
We all love Linux, but it's also a fact that some people might not be able to migrate.
Re: Many thanx, Lambda
it really is irrelevant that the book of quotations was published by indians, or whoever. the legend has been around for several centuries, you'll find the story repeated everywhere. and, yes, you will find muslims who believe it's a factual account.zeeshan Hussain wrote: second..after reading above and few other articles i am dropping that quote off.
i read that in the Dictionary of Quotations, published by indians.
i'm not saying it's not true  it may very well be true.
Who is online
Users browsing this forum: No registered users and 1 guest