Click here to Skip to main content
15,908,437 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
i want some explanation of this program and how it works.

C#
#include <stdio.h>

int main(void)
{
  int num, i, is_prime;

  printf("Enter a number: ");
  scanf("%d", &num);


  is_prime = 1;

  for(i = 2; i <= num / 2; i = i + 1)
    if((num%i)==0)
         is_prime = 0;

  if(is_prime==1)
      printf(" is prime.");
  else
      printf("not prime.");

  return 0;
}
Posted
Comments
Stefan_Lang 22-Oct-12 10:33am    
If the person who downvoted this question reads this: everyone has to start somewhere! Just because it is trivial to a seasoned programmer, is no reason to downvote this question.

Also: if there is a valid reason for downvoting, at least post a comment and point it out! Otherwise the vote is utterly pointless.

P.S. to the author: Your question wasn't particularly specific, and that may have been the reason why someone voted it down. In any case, now that you have some solutions, please accept them as solution if they answer your question, or enter a comment if you have further questions.

It's basically a simple trial division prime finder. See here: http://en.wikipedia.org/wiki/Trial_division[^] which explains it very well.
 
Share this answer
 
C#
int num, i, is_prime; // declare three variables, num for user input, i for loop and is_prime used as boolean for the result

//get number from user
 printf("Enter a number: ");
 scanf("%d", &num);

//set is_prime to true
 is_prime = 1;

 for(i = 2; i <= num / 2; i = i + 1) //start from 2 and reach until the half the number
   if((num%i)==0)// check if any of the number in for loop range successfully divides the number, if so set is_prime to false, as dividable no are not prime
        is_prime = 0;

 if(is_prime==1) // check if Is_prime was never changed in loop, if s
     printf(" is prime.");
 else // if the is_prime was changed
     printf("not prime.");



most of time we use
C#
for(i = 2; i <= num / 2; i = i + 1)
    if((num%i)==0){
         is_prime = 0;
         break; }

this stops the for loop as soon as the no is found not to be prime, save remaining iterations of loop
 
Share this answer
 
Comments
Stefan_Lang 22-Oct-12 10:28am    
Agreed on the optimization, but I'd put it in the condition like that:
for(i = 2; i <= num / 2 && is_prime != 0; i = i + 1)
(and this isn't the only possible optimization by far ;-) )
psychic6000 22-Oct-12 11:03am    
i disagree with your optimization, in each reputation of this for loop, two comparison are being performed (one in for loop, 2nd in IF statement), in yours comment comparisons become three...
although you are right there are many other ways to increase performance...
Stefan_Lang 23-Oct-12 3:43am    
Well, I didn't say that suggestion was meant for optimization ;-) It was meant for maintenance: if you look t that code later, it is much easier to decide just how long a loop will run if the stopping condition is in just one place, rather than spread out over the code of the loop. Granted, it is trivial in this case, but code tends to grow over time, so it is a good practice to keep your code clean right from the start.
[no name] 22-Oct-12 16:15pm    
thanks alot psychic600 for ur answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900