YouTip LogoYouTip

Cpp Examples Prime Number

C++ Example – Check for Prime Number |

-- Learning more than just technology, but dreams!

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 ">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++ Algorithm Library ">C++ Algorithm Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Multithreading Library ">C++ Multithreading Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Exception Handling Library ">C++ Exception Handling Library <a href="#" title="C++ Exception Handling Library ">C++ Exception Handling Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Memory Management Library ">C++ Memory Management Library <a href="#" title="C++ Memory Management Library ">C++ Memory Management Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library <a href="#" title="C++ Standard Library ">C++ Standard Library C++ OpenCV

C++ Useful Resources C++ Quiz

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

C++ Example C++ Example

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

C++ Example C++ Example

← Cpp Examples Hcf GcdCpp Examples Leap Year β†’