## Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of posts: 16951

This Forum Only

program to find prime number between 2 and any given integer and print Posted by yak98 on 16 Jun 2012 at 8:20 AM
hi
i am new to c.i tried to write a program to find all prime numbers between 2 and any given number and print them.i am attaching the source code.but everytime i run it,garbage value appears.can anyone help me with it?
Re: program to find prime number Posted by ishan_2k on 18 Jun 2012 at 7:56 AM
hello, following is the program for finding prime number it also prints about other number are prime number or not. if you want to print only prime numbers just remove last printf statement of the program.and "is a prime number" in previous printf statement.
"&" in printf statement in while loop.

#include<stdio.h>
main()
{
int check, i, x,y,m;
printf("enter the number upto which check");
scanf("%d",&check);
y=1;
for(m=2; m<=check; m++)
{
for(x=2;x<m; x++)
{ y=m%x;
if(y==0)
break;
}
if(y!=0)
printf("%d is prime number\n",m);
else printf("%d is not a prime number\n",m);
}
getch();
}

Re: program to find prime number Posted by ishan_2k on 18 Jun 2012 at 7:58 AM
hello, following is the program for finding prime number it also prints about other number are prime number or not. if you want to print only prime numbers just remove last printf statement of the program.and "is a prime number" in previous printf statement.
"&" in printf statement in while loop.

#include<stdio.h>
main()
{
int check, i, x,y,m;
printf("enter the number upto which check");
scanf("%d",&check);
y=1;
for(m=2; m<=check; m++)
{
for(x=2;x<m; x++)
{ y=m%x;
if(y==0)
break;
}
if(y!=0)
printf("%d is prime number\n",m);
else printf("%d is not a prime number\n",m);
}
getch();
}

Re: program to find prime number Posted by ishan_2k on 18 Jun 2012 at 7:59 AM
hello, following is the program for finding prime number it also prints about other number are prime number or not. if you want to print only prime numbers just remove last printf statement of the program.and "is a prime number" in previous printf statement.
"&" in printf statement in while loop.

#include<stdio.h>
main()
{
int check, i, x,y,m;
printf("enter the number upto which check");
scanf("%d",&check);
y=1;
for(m=2; m<=check; m++)
{
for(x=2;x<m; x++)
{ y=m%x;
if(y==0)
break;
}
if(y!=0)
printf("%d is prime number\n",m);
else printf("%d is not a prime number\n",m);
}
getch();
}

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic