How To Find Prime Numbers In Dev C++

How to find prime numbers in dev c online

For example 13 is a prime number because it is only divisible by itself and 1, while 14 is not a prime number because it is divisible by 2 and 7 along with the number itself and 1. This program takes the input number and checks whether the number is prime number or not using a function. A prime number is a number that is greater than 1, and there are only two whole-number factors 1 and itself. Example of prime numbers are - 2, 3, 5, 7, 11, 13, 17, 19, 23 etc. C Program to Check whether a Number is Prime or not. C, C Program to Check whether a Number is prime or not.

A prime number is a numeral that is greater than 1 and cannot be divided evenly by any other number except 1 and itself. If a number can be divided evenly by any other number not counting itself and 1, it is not prime and is referred to as a composite number.

Factors vs. Multiples

Prime Number Program C

When working with prime numbers, students should know the difference between factors and multiples. These two terms are easily confused, but factors are numbers that can be divided evenly into the given number, while multiples are the results of multiplying that number by another.

Additionally, prime numbers are whole numbers that must be greater than one, and as a result, zero and 1 are not considered prime numbers, nor is any number less than zero. The number 2 is the first prime number, as it can only be divided by itself and the number 1.

Using Factorization

Using a process called factorization, mathematicians can quickly determine whether a number is prime. To use factorization, you need to know that a factor is any number that can be multiplied by another number to get the same result.

For instance, the prime factors of the number 10 are 2 and 5 because these whole numbers can be multiplied by one another to equal 10. However, 1 and 10 are also considered factors of 10 because they can be multiplied by one another to equal 10. In this case, the prime factors of 10 are 5 and 2, since both 1 and 10 are not prime numbers.

An easy way for students to use factorization to determine if a number is prime is by giving them concrete counting items like beans, buttons, or coins. They can use these to divide objects into ever-smaller groups. For example, they could divide 10 marbles into two groups of five or five groups of two.

Using a Calculator

After using the concrete method as described in the previous section, students can use calculators and the concept of divisibility to determine whether a number is prime.

Have students take a calculator and key in the number to determine whether it is prime. The number should divide into a whole number. For example, take the number 57. Have students divide the number by 2. They will see that the quotient is 27.5, which is not an even number. Now have them divide 57 by 3. They will see that this quotient is a whole number: 19. So, 19 and 3 are factors of 57, which is, then, not a prime number.

Other Methods

Another way to find if a number is prime is by using a factorization tree, where students determine the common factors of multiple numbers. For instance, if a student is factoring the number 30, she could begin with 10 x 3 or 15 x 2. In each case, she continues to factor—10 (2 x 5) and 15 (3 x 5). The end result will yield the same prime factors: 2, 3, and 5 because 5 x 3 x 2 = 30, as does 2 x 3 x 5.

Simple division with pencil and paper can also be a good method for teaching young learners how to determine prime numbers. First, divide the number by 2, then by 3, 4, and 5 if none of those factors yields a whole number. This method is useful to help someone just starting out to understand what makes a number prime.

In this program, you will learn about C++ program to check prime number in two different ways.

First, let’s be clear about the prime number:

A number is called prime number if it is divisible by 1 and itself only.

For example: 1, 3, 5, …., 91, ….

Example 1: C++ program to check prime number

Output

Explanation

The logic of the above program is to check the number entered by the user is perfectly divisible by 1 and itself which is accomplished inside for loop.

The iteration continues till the value of i equals the number.

Let’s check out another way of solving the same problem.

Example 2: Prime number program in C++

Output

How to calculate prime numbers c

Explanation

This program also checks whether the number entered by the user is perfectly divisible by i or not.

How To Calculate Prime Numbers C

In this case, we have declared and initialized boolean variable checkPrime to true. If the num is perfectly divisible by i then checkPrime is set to false.