C++ is a programming language that is used for creating software applications. It is based on the C programming language, and it is similar to C++.
A prime number is a number that is divisible only by itself and 1. For example, 3 is a prime number because 3 is divisible only by 1 and itself. However, 4 is not a prime number because 4 is divisible by 2, which is not a prime number.
In this article, we will look at prime number programming in C++. Prime number programming is a simple concept, but it’s incredibly useful. Prime numbers are numbers that have no factors other than themselves and 1.
For example, 2 is a prime number because it has no factors except for itself and 1. But 3 isn’t a prime number because it has factors other than itself and 1.
![Prime Number Program In C++ 2 Prime Number Program in C++](https://www.codewithc.com/wp-content/uploads/2022/06/Prime-Number-Program-in-C-1024x576.jpg)
And in fact, there are infinitely many prime numbers. We can use a program to help us find all of them.
#include
#include
using namespace std;
int main()
{
vector primes;
for(int i = 2; i < 1000; i++)
{
if(primes.empty() || primes[primes.size() - 1] % i!= 0)
{
primes.push_back(i);
}
}
for(int i = 0; i < primes.size(); i++)
{
cout << "The " << i + 1 << "st prime number is " << primes[i] << endl;
}
return 0;
}
Example 2
#include
#include
using namespace std;
int main()
{
int n, count = 0, i, j;
cout << "Enter the number to be checked: ";
cin >> n;
if (n == 2)
{
cout << "Number is even" << endl;
return 0;
}
if (n % 2 == 0)
{
count++;
}
for (i = 3; i * i <= n; i += 2)
{
if (n % i == 0)
{
count++;
}
}
if (count == 1)
{
cout << "Number is prime" << endl;
return 0;
}
else
{
cout << "Number is not prime" << endl;
return 0;
}
}
What is the prime factorization of a number?
The prime factorization of a number is nothing but a representation of the numbers used to divide the number. It is nothing but the product of the prime numbers used to divide the number.
Another Example to print prime number in C++ program
In simple words, prime numbers are the numbers which cannot be divided by any other number except 1 and itself.
For example, 2, 3, 5, 7, 11, 13 are prime numbers, but numbers like 2, 6, 8, 9 are not.
Now let’s see how we can write the prime number program in C++.
Steps to Write the Prime Number Program in C++:
Write the main function.
If you don’t know how to write the main function, then follow the below mentioned instructions.
The main function is the starting point of a C++ program. It is the first thing that is going to get executed.
So, you need to write the main function, which will tell the computer that the program is running.
The name of the program is prime.
The file name is prime.cpp.
The main function is written as follows:
int main()
{
printf("Enter the number : ");
int num = 0;
scanf("%d", &num);
if (num == 1)
{
printf("\nYou Entered a prime number.");
}
else if (num % 2!= 0)
{
printf("\nNumber is not a prime number.");
}
else if (num > 1 && num < 100)
{
while (num > 1)
{
if (num % 2 == 0)
break;
num = num / 2;
}
printf("\nThe prime number is : %d", num);
}
return 0;
}
Conclusion:
As we have seen in the above instructions that you don’t need any programming knowledge to write the prime number program in C++.
I am sure that you will be able to complete the given task. The only thing that you need to do is to follow the steps in the right sequence.