Program to print all prime numbers btw 1 to 100

C Program to print all prime numbers btw 1 to 100..
pls explain the program: What is if(p)??

Code
  1. #include

  2. #include

  3. "Enter any number: ""%d""       %d"
Copyright GeekInterview.com

Questions by Pavani Shiny

Showing Answers 1 - 3 of 3 Answers

Rajkumar Yonzan

  • Feb 24th, 2017
 

// Wap to display all prime number from 1 to 100. //

#include
#include
void main()
{
int i,j,n;
for(n=1;n<=100;n++)
{
i=0;
for(j=1;j<=n;j++)
{
if(n%j==0)
i++;
}
if(i==2)
printf("
%d",n);
getch();
}
}

  Was this answer useful?  Yes

To answer the specific question, "if(p)" is the same thing as "if (p == 1)" or "if (p != 0)". The variable "p" is being used as a flag to indicate whether the current candidate number ("n") is prime or not. Thats being determined by using the modulo operator %. If "a % b == 0", then "a" is evenly divisible by "b". So the program performs "n % div" for all values of "div" from 2 to ... something (that part of the code appears to be missing). If "n % div != 0" for all values of "div", then the number is prime and "p" is set to 1 (true). If "n % div == 0" for any value of "div", then the number is "not" prime and "p" is set to 0 (false).

Code
  1.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions