Wednesday, October 14, 2015

Write a program to check a number is a Prime number or Not?


Theory
Prime Number:  prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. 

A natural number greater than 1 that is not a prime number is called a composite number

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. 

The fundamental theorem of arithmetic establishes the central role of primes in number theory

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.


As of September 2015, the largest known prime number has 17,425,170 decimal digits.


PROGRAM OF PRIME NUMBER IN C

#include<stdio.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. 
     }  
   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); 
    }
   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