Customizable training materials to learn C++

Looking for C++ Tutorial Visit Nano teach c++!
  • C++ Compute the Sum and Average of Two Numbers
    03/07/2014 - 0 Comments
    This program takes in two integers x and y as a screen input from the user. The sum and average of these two integers are calculated and outputted using the ‘cout’…
  • C++ Objects and Classes
    08/07/2014 - 0 Comments
    In object-oriented programming languages like C++, the data and functions (procedures to manipulate the data) are bundled together as a self-contained unit called an object. A…
Nano teach C++ found result for your search here:

C++ Program to find 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>
#include<math.h> //sqrt function used

using namespace std;

int main(){
// Variable Declaration 
int n;

// Get Input Value
 cout<<"Enter the Number : ";
 cin>>n;

cout <<"List Of Prime Numbers Below "<<n<<endl;

//for Loop Block For Find Prime Number 

for (int i=2; i<n; i++)
 for (int j=2; j*j<=i; j++)
 {
    if (i % j == 0)
    break;
    else if (j+1 > sqrt(i)) {
    cout << i << endl;
 }
} 

// Wait For Output Screen
 system("pause");
 return 0;
}
Program Output:
Enter the Number : 50
List Of Prime Numbers Below 50
5
7
11
13
17
19
23
29
31
37
41
43
47

No comments:

Post a Comment