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;
}

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?

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

commented: GOTO jail, don't collect $200. Where's that free jail card when needed? ;) +9

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

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.

#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;
}

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.

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
   }
}

@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.

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

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

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

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

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.

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.

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...)

a

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

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.

#include<stdio.h>
#include<conio.h>
void main()
{
 int flag=0;
 int a=11,i,temp,temp=a;
 for(i=1;i<=temp;i++)
 {
if((a%i)==0)
{flag++;
}
}
if(flag==2)
{
 printf("prime");
 }

Algorithm to check whether a number(N) is prime number or not.
For every number i, between 2 to N/2(2<= i <= N/2) check whether i divides N completely(check If i is a factor of N).
* if (N % i == 0), then N cannot be a Prime number.
* If none of the number between 2 to N/2 divides N completely then N is a prime number.
Below program prints all prime numbers between 1 to K.

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

int main(){

    int N, i, j, isPrime, n;

    printf("To print all prime numbers between 1 to N\n");
    printf("Enter the value of N\n");
    scanf("%d",&N);

    /* For every number between 2 to N, check 
    whether it is prime number or not */
    printf("Prime numbers between %d to %d\n", 1, N);

    for(i = 2; i <= N; i++){
        isPrime = 0;
        /* Check whether i is prime or not */
        for(j = 2; j <= i/2; j++){
             /* Check If any number between 2 to i/2 divides I 
              completely If yes the i cannot be prime number */
             if(i % j == 0){
                 isPrime = 1;
                 break;
             }
        }

        if(isPrime==0 && N!= 1)
            printf("%d ",i);
    }
   getch();
   return 0;
}

Source : Print prime numbers in C

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.