Saturday, 18 November 2017

Programs on C - All three digit prime numbers

A prime number is a number which is either divisible by 1 or itself, hence, we need to check if the number is divisible by any number but itself and 1. If the remainder is zero on checking this condition then the number is not a prime number.

#include <stdio.h>

int main()

{

int i, j;

int IsPrime;

printf("The three digit prime numbers are:\n");

for(i=100;i<1000;i++)

{

IsPrime = 1;

for(j=2;j<i;j++) //check divisibility with all but 1 and itself

{



if(i%j==0) //if divisible then it's not a prime number

{

IsPrime = 0; //not a prime number

break; //no need to check divisibilty further as it's divisible by some no.

}

}

if(IsPrime==1)

{

printf("%d \n", i); //print only if the no. is not divisible by any other no. except itself or 1, i.e. it's prime no.

}

}

return 0;

}


No comments:

Post a Comment