c++ calculate prime numbers from 1 to 100

c++ calculate prime numbers from 1 to 100

When I took the C++ practice test, there was a problem of calculating the prime number of 1-100, but I didn’t figure it out.

Since the exam uses the Dev-C++ development tool, in order to prepare for the next exam, use this tool instead, just download and install it directly, no cracks will be involved, etc.

Download link: https://sourceforge.net/projects/orwelldevcpp/

New Construction

Choose Console Application

It can be written directly afterwards, which is much easier than VC++ 6.0, and much lighter than Visual Studio (aircraft carrier)

Core: Exclude all numbers that can be divisible by k (the remainder is 0) before i=k

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
    
    for (int i = 2; i <= 100; i++) {//prime number
        for (int k = 2; k <= i; k++) {//divisor
           //Exclude all numbers that can be divisible by k (the remainder is 0) before i=k
            if (i% k == 0 && i != k) 
                break;
           //Output all the numbers in i=k and i%k=0
            if (i% k == 0 && i == k) 
                cout << i << endl;
        }
    }
    
    return 0;
}

f11 compile and run

result

On the whole, there is no difference between this code java, that is, the output mode has changed. ok, for the time being!

Reference: https://cloud.tencent.com/developer/article/1495770 c++ calculates prime numbers within 1 to 100-Cloud + Community-Tencent Cloud