
This is the most basic method of checking the primality of a given integer num and is called trial division.įinally, based on the return value of isPrimeNumber function, we display message on screen saying whether number is prime number or not. If num is divisible by any number between 2 and num/2 then num is not a prime number. To test whether a number is prime or not we are using brute force approach by testing whether num is a multiple of any integer between 2 and num/2. If number is prime then it returns true otherwise false. Here we defined a function isPrimeNumber which check whether a number is prime number or not. We then call isPrimeNumber function by passing num to check whether num is prime number or not. In this program, we first take an integer as input from user using cin and store it in a variable num. Check whether num is divisible by any number between 2 to (num/2) All numbers other than prime numbers are known as composite numbers.įirst few prime numbers are : 2 3 5 7 11 13 17 19 23 29 … C++ program to check a prime number using function In other words, a prime is not divisible by any other number other than itself. Here s the formal definition of prime numbers:Ī Prime number is a natural number greater than 1 that is only divisible by either 1 or itself. In this program, we will learn about prime numbers and how to check whether a number is prime number or not. Write a C++ program to check whether a number is prime number or not using function.C++ Program to Check Prime Number Using Function In this article, we will see C++ Program to Check Prime Number Using Function. Prime number in c++ using function: In the previous article, we have discussed C++ Program to Check Prime Number.
