Theory
Prime Number:
A prime number (or
a prime) is a natural
number greater than 1 that has no
positive divisors other
than 1 and itself.
For example, 5 is prime because 1 and 5 are its
only positive integer factors, whereas 6 is composite because it has the
divisors 2 and 3 in addition to 1 and 6.
One
is not a Prime Number: Any integer greater than 1 can be expressed as a product
of primes that is unique up
to ordering. The uniqueness in
this theorem requires
excluding 1 as a prime
because one can include arbitrarily many instances of 1 in any factorization,
e.g., 3, 1 · 3, 1 · 1 · 3, etc. are all valid factorizations of 3.
The
property of being prime (or not) is called primality.
PROGRAM OF PRIME
NUMBER IN C
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
int n, i=3, c;
printf("Enter the number of prime numbers required\n");
scanf("%d",&n);
if ( n <=1 )
{
printf("\n%d is not a Prime number: ",n);
exit(0); //It is a Predefined Funtion in stdlib.h header file. To Normally Terminate the program.
}
for ( c = 2 ; c <n ; c++ )
{
if ( n%c == 0 )
break; // break will take execution out of the loop.
}
for ( c = 2 ; c <n ; c++ )
{
if ( n%c == 0 )
break; // break will take execution out of the loop.
}
if ( c == n)
// If my number is a prime it will successfully increment the value of c to n.
{
printf("\n%d Is a Prime number:\n",n);
}
{
printf("\n%d Is a Prime number:\n",n);
}
else //If My number is not a prime number
it will never be equal to n.
{
printf("\n%d is not a Prime number: \n",n);
}
getch();
return 0;
}
No comments:
Post a Comment