C++ Example β Check for Prime Number |
-- Learning more than just technology, but dreams!
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
C++ Tutorial
C++ Tutorial C++ Introduction C++ Environment Setup C++ Basic Syntax C++ Comments C++ Data Types C++ Variable Types C++ Variable Scope C++ Constants C++ Modifier Types C++ Storage Classes C++ Operators C++ Loops C++ Decisions C++ Functions C++ Numbers C++ Arrays C++ Strings C++ Pointers C++ References C++ Date & Time C++ Basic I/O C++ struct C++ vector Container C++ Data Structures
C++ Object-Oriented
C++ Classes & Objects C++ Inheritance C++ Overloading C++ Polymorphism C++ Data Abstraction C++ Data Encapsulation C++ Interfaces (Abstract Classes)
C++ Advanced
C++ Files and Streams C++ Exception Handling C++ Dynamic Memory C++ Namespaces C++ Templates C++ Preprocessor C++ Signal Handling C++ Multithreading C++ Web Programming
C++ Resources
C++ STL Tutorial C++ Import Standard Library C++ Standard Library C++ Useful Resources C++ Examples C++ Quiz <a href="#" title="C++ Standard I/O β ">C++ Standard I/O β <a href="#" title="C++ File I/O Library β ">C++ File I/O Library β <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Container Class ">C++ Container Class <a href="#" title="C++ Container Class ">C++ Container Class <a href="#" title="C++ Container Class ">C++ Container Class <a href="#" title="C++ Container ">C++ Container <a href="#" title="C++ Container Class ">C++ Container Class <a href="#" title="C++ Container Class ">C++ Container Class <a href="#" title="C++ Container Class ">C++ Container Class <a href="#" title="C++ Container Class ">C++ Container Class <a href="#" title="C++ Container Class ">C++ Container Class <a href="#" title="C++ Container Class ">C++ Container Class <a href="#" title="C++ Container Class
Explore Further
- Web Services
- Programming
- Software
- Scripting Languages
- Web Design & Development
- Computer Science
- Web Service
- Programming Languages
- Scripts
- Development Tools
C++ Example - Check for Prime Number
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Example
#include<iostream>
using namespace std;
int main()
{
int n, i;
bool isPrime = true;
cout << "Enter a positive integer: ";
cin >> n;
for(i = 2; i <= n / 2; ++i)
{
if(n % i == 0)
{
isPrime = false;
break;
}
}
if(isPrime)
cout << "is a prime number";
else
cout << "is not a prime number";
return 0;
}
The output of the above program is:
Enter a positive integer: 29
is a prime number
YouTip