Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

C++ program that prints the prime numbers from 1 to 1000.

#include <iostream>
#include<cmath>
using namespace std;

int main()
{   cout << "Prime Numbers between 1 and 100 are:
";

    for(int i=2;i<=100;++i) //loop to check for each number in the range

    {   int ctr=0; //to maintain factor count

        for(int j=2;j<=sqrt(i);++j) //checking for factors

        {   if(i%j==0)

                ctr=1; //increasing factor count when found

        }

        if(ctr==0) //checking and printing prime numbers

                cout<<i<<" ";

    }

    return 0;

}
Source by www.studymite.com #
 
PREVIOUS NEXT
Tagged: #program #prints #prime #numbers
ADD COMMENT
Topic
Name
3+3 =