1,105,585 Community Members

Program to print prime numbers in c

Member Avatar
shubhamgore
Newbie Poster
9 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-6
 

hi friends
this small compact code is for printing any number of prime numbers in C Language, you just have to change the last line's 500 to any number you want,
here some points necessary to understand the code are as follows-

1.> variable torf stands for true or false,in case if number is prime(true condition), torf's value is 1, else it is zero.

2.> here i have used for loop for simplicity, you can modify this if you lOVE TO HANDLE complications. :cool:

for any other help, regarding computing, feel free to contact-
<<snip>>

SHUBHAM GORE, B.tech (1st YEAR)
IIIT- JABALPUR,
M.P.

#include<stdio.h>
void main()
{
int i=2,j,torf;
shubhamgore_08109075922_india:
torf=1;
for(j=2;j<i;j++) if(i%j==0) torf=0;
if(torf==1) printf("\n\t %d is a Prime number",i);
i++;
if(i<500) goto shubhamgore_8109075922_india;
}
Member Avatar
Nick Evan
Industrious Poster
4,827 posts since Oct 2006
Reputation Points: 4,005 [?]
Q&As Helped to Solve: 560 [?]
Skill Endorsements: 30 [?]
Team Colleague
Featured
 
1
 

So where is it?

[edit] Ah there it is, my browser apparently had a small err.

It's not very good, to say the least... goto, void main, no indention, use of stdafx...
Where exactly did you learn C?

Member Avatar
abhimanipal
Master Poster
746 posts since Dec 2009
Reputation Points: 91 [?]
Q&As Helped to Solve: 104 [?]
Skill Endorsements: 0 [?]
 
1
 

Logically Incorrect
void main as opposed to int main
Unnecessary use of goto

Member Avatar
shubhamgore
Newbie Poster
9 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i am extremely sorry,
i would rectify your criticism shortly...

Member Avatar
shubhamgore
Newbie Poster
9 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Nick and abhimanipal, thanks,i really appreciate feedback from senior members like you, i want to assure you that i will definitely improve the code, shortly.

Member Avatar
shubhamgore
Newbie Poster
9 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
#include<stdio.h>
int main()
{
int i=2,j,torf;
star:
torf=1;
for(j=2;j<i;j++)
{
 if(i%j==0)
{
 torf=0;
}
}
if(torf==1)
{
 printf("\n\t %d is a Prime number",i);
}
i++;
if(i<500) goto star;
return 0;
}
Member Avatar
shubhamgore
Newbie Poster
9 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

plz help me to remove goto.
and please tell me why should we have to avoid goto, i have used it here becoz i have learn it yesterday from complete reference (c) [fundamental book for c language], and there was a note, saying that whenever we want to shift control of program over any part of the code, we can use goto.
Thanks in Advance,
I am expecting a detailed answer from my senior members, please please help me.

Member Avatar
nbaztec
Posting Pro in Training
473 posts since May 2010
Reputation Points: 45 [?]
Q&As Helped to Solve: 62 [?]
Skill Endorsements: 0 [?]
 
0
 

we can does not imply we have to
First 2 is the only EVEN prime. Your program mentions it as Not Prime.
Second unnecessarily checking torf for 1, when torf is just a replacement for boolean flag. You can pretty much use it as

if(torf)

Third your program can make use of another loop.
I give you a somewhat pseudo-code.

for(i=2 to 500)
{
   if(i==2)
      PRINT 2;
   else
   {
      for(j=2; j LESS THAN i)
         if(i%j==0)
            torf=0;
      if(torf)
         PRINT PRIME
   }
}
Member Avatar
NP-complete
Junior Poster
174 posts since May 2010
Reputation Points: 42 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 4 [?]
 
0
 

@nbaztec and others

I think the main loop that checks for primes can be iterated from:

for (int i = 2; i <= n/2; i++) //where n is the number to be checked for

I think it'll be a bit faster.
And also, using break may also reduce unnecessary iterations.

Member Avatar
abhimanipal
Master Poster
746 posts since Dec 2009
Reputation Points: 91 [?]
Q&As Helped to Solve: 104 [?]
Skill Endorsements: 0 [?]
 
0
 

You dont need to go all the way upto n/2. Just check utpo square root of n.
google what are the pros and cons of using goto

Member Avatar
NP-complete
Junior Poster
174 posts since May 2010
Reputation Points: 42 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 4 [?]
 
0
 

oh...true, true...that was a bad mistake (or, not a good optimisation)...:)

Member Avatar
shubhamgore
Newbie Poster
9 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ok, thanx, i have got a new logic for the same problem and itz too better. but before posting, i will analyse it myself.

Member Avatar
shubhamgore
Newbie Poster
9 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

EXCELLENT Job nbaztec , thanx man...
you are too good.

Member Avatar
0x69
Junior Poster
130 posts since Apr 2010
Reputation Points: 41 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

why should we have to avoid goto, i have used it here becoz i have learn it yesterday from complete reference (c) [fundamental book for c language], and there was a note, saying that whenever we want to shift control of program over any part of the code, we can use goto.

In some situations goto makes life easier to developer - for example if we need to abandon processing in deeply nested code with many loops,-

For ...
 For ...
   While ...
     if (error) goto Error_label

Otherwise without goto we would depend on additional variable which will tell to each loop- Do we need to execute it or not. So in some cases goto lets to ignore above code and lets you concentrate on your current needs. But this has BAD side too. When your projects gets bigger and bigger, the use of more and more goto's will make your code some sort of un-predictable nature and it will be hard for you to maintain such code. So in one way or the other in the long term you will want yourself to eliminate every use of goto. Especially given the fact that there are no such case of goto which couldn't be eliminated. So in the result - goto is considered as archaic and unnecessary feature in most programming languages.

Member Avatar
nbaztec
Posting Pro in Training
473 posts since May 2010
Reputation Points: 45 [?]
Q&As Helped to Solve: 62 [?]
Skill Endorsements: 0 [?]
 
0
 

In some situations goto makes life easier to developer - for example if we need to abandon processing in deeply nested code with many loops,-

For ...
 For ...
   While ...
     if (error) goto Error_label

Otherwise without goto we would depend on additional variable which will tell to each loop- Do we need to execute it or not. So in some cases goto lets to ignore above code and lets you concentrate on your current needs. But this has BAD side too. When your projects gets bigger and bigger, the use of more and more goto's will make your code some sort of un-predictable nature and it will be hard for you to maintain such code. So in one way or the other in the long term you will want yourself to eliminate every use of goto. Especially given the fact that there are no such case of goto which couldn't be eliminated. So in the result - goto is considered as archaic and unnecessary feature in most programming languages.

Nice explanation a la spaghetti code.

Member Avatar
0x69
Junior Poster
130 posts since Apr 2010
Reputation Points: 41 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

Huh, I will correct myself regarding this-

Otherwise without goto we would depend on additional variable which will tell to each loop- Do we need to execute it or not

We not necessary need additional variable here. We could simply use standard assert() macro OR we could implement elegant exception handling in C :
http://en.wikipedia.org/wiki/Exception_handling_syntax#C

So this clearly shows that there are many ways to get rid of goto ;)
(Actually C exception handling uses setjmp() and longjmp() which functions like non-local goto ;) So maybe we should also get rid of exception handling in C...)

Member Avatar
shubhamgore
Newbie Poster
9 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

a

Member Avatar
shubhamgore
Newbie Poster
9 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanxx, 0x69.

Member Avatar
stanleyet
Newbie Poster
1 post since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

c=1
for(j=2;j<=n-1;j++)
{
if(n%j==0)
c=0
}
if c==1
print prime
else print not prime

try it!!

u can use n/2 also in for loop. it is a bit fast

Member Avatar
newbie_learner
Newbie Poster
6 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i would like to recommend you the book "Let Us c " by Mr. Yashwantrao Kanetkar. This is book is very popular and also very useful too if you really want to do c programming also try solving all the programs in it as it really clears the understanding of each and every aspect of c language.

You
Post:
Start New Discussion
Tags Related to this Article