Friday 1 July 2016

C++ Program to Print Prime Numbers from 1 to N

C++ Program to Print Prime Numbers from 1 to N

Description: Written below is a C++ program for prime number which prints a prime numbers list from 1 to N e.g. prime numbers from 1 to 100.


#include <iostream>
using namespace std;

int main()
{
     int input, mod = 1, i = 2, j = 2;
     cout << "Please enter the number to which you want to know the prime numbers: ";

     cin >> input;

     for (i = 2; i <= input; i++){
           for (j = 2; j<i; j++){
                mod = i%j;
                if (mod == 0)
                     break;
           }

           if (mod != 0)
                cout << i << " ";
     }

     cout << endl;

     return 0;
}


No comments

Post a Comment

Recent Posts