C Program to Print Prime Numbers Upto N

In this program, we will display the prime number from 1 to N provided by user.

Prime numbers are special numbers, greater than 1, that have exactly two factors, themselves and 1.

19 is a prime number. It can only be divided by 1 and 19.

The prime numbers below 20 are: 2, 3, 5, 7, 11, 13, 17, 19. ...



Source Code
#include <iostream>
using namespace std;

int main()
{
    int i, start = 1, end = 50, isPrime;
    cout<<"Prime numbers between 1 to 50 are: \n";

    while (start <= end)
    {
        isPrime = 1;

        for(i = 2; i <= start/2; ++i)
        {
            if(start % i == 0)
            {
                isPrime = 0;
                break;
            }
        }

        if (isPrime == 1)
            cout<< start << ", ";

        ++start;
    }
    return 0;
}
Output
Prime numbers between 1 to 50 are:
1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,