The method is simple, divide the prime number by integers ranging from 2 to number-1. If the division generates a whole number(that is remainder is 0), then it is not a prime number, as they are only divisible by themselves(and 1) and not by any other number.
For e.g if we want to check whether 5 is a prime number or not(which it is :p ), we simply divide 5 by numbers less than 5 i.e 2,3,4 . Since the division will not generate a whole number, hence it is a prime number. In the language of coding, simply run a loop from 2 to number-1 and divide it with the number itself. If the remainder is 0 then it is not a prime no. If it is 1 then it is a prime number. Simple !! :)
The following program demonstrates the above method :
- The only even prime number is 2. All other even numbers can be divided by 2.
- If the sum of a number's digits is a multiple of 3, that number can be divided by 3.
- No prime number greater than 5 ends in a 5. Any number greater than 5 that ends in a 5 can be divided by 5.
- Zero and 1 are not considered prime numbers.
- Except for 0 and 1, a number is either a prime number or a composite number. A composite number is defined as any number, greater than 1, that is not prime.
Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
3 - Fibonacci series in C++ : http://programsplusplus.blogspot.in/2012/03/fibonacci-series-in-c.html