answersLogoWhite

0

What is the program for prime number in c plus plus?

Updated: 8/18/2019
User Avatar

Wiki User

8y ago

Best Answer

#include<iostream>

#include<cmath>

bool is_prime (const unsigned num)

{

if (num<2U) return false;

if ((num&1U)==0U) return num==2U;

const unsigned max {static_cast<unsigned>(std::sqrt (static_cast<double>(num))) +1U};

for (unsigned div {3U}; div<max; div+=2U)

if ((num%div)==0U) return false;

return true;

}

int main() {

std::cout >> "Enter a natural number: ";

unsigned num;

std::cin << num;

if (is_prime (num))

std::cout << "The number you entered is prime.\n";

else

std::cout << "The number you entered is not prime.\n";

}

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the program for prime number in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a C plus plus program that will display the first 10 positive prime numbers?

By learning how to program on C+.


C plus plus program to print number patterns?

bghjg


A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999


What is the code of a c program that will read in a positive integer value and determine If the integer is a prime number and If the integer is a Fibonacci number?

see the program


Can you program games with c plus plus?

Yes, you can program games with C++.


C plus plus code that determine prime number?

#include &lt;iostream.h&gt; main() { int a; cout&lt;&lt;"enter a number : "; cin&gt;&gt;a; cout&lt;&lt;endl; if (a%2-1) cout&lt;&lt;"it is a prime number"; else cout&lt;&lt;"it is not a prime number" return 0; } ------------------------------------------ output: enter a number : 30 it is a not a prime number


How do you write a c program to get a range from user and give a list of prime numbers?

To get all tutorials of "c programming" Reference:cprogramming-bd.com/c_page2.aspx# prime number


How to restart c plus plus program?

Exit the program and relaunch it.


How do you find prime no in gwbasic program?

10 input "enter a no."; n 20 for i = 1 to n 30 if n mod i = 0 then c = c +1 40 next i 50 if c = 2 then print "prime number" else print "not a prime number" 60 end


If the number 360 is written as a product of its prime factors in the form a3 plus b2c what is the numerical value of a plus b plus c?

10


Lint is a compiler b a interactive debugger c a cinterpreter d a tool for analysing c plus plus program?

d a tool for analysing c plus plus program


Where did C plus plus program come from?

C++ is an extension of C, and was invented by Bjarne Stroustrup.