C solves all prime numbers within 100 and outputs them

Updated on technology 2024-02-28
4 answers
  1. Anonymous users2024-02-06

    #include

    #include

    void main()

    int m,k,n=0;

    for(m=1;m<=100;m=m+2) because even numbers are certainly not prime, so it is just necessary to judge the case of odd numbers, and the algorithm simplification.

    k=sqrt(m);sqrt is open squared.

    for(i=2;i<=k;i++)

    if(m%i==0)break;

    if(i>=k+1)

    printf("%d",m);

  2. Anonymous users2024-02-05

    If you don't care about efficiency at all, you can write it like this:

    #include

    int main(){

    int i,j;

    for(i=2;i<=100;i++)

    for(j=2;j<2;j++)

    if(i%j==0) break;

    if(i==j) printf("%d",i);

    return 0;

  3. Anonymous users2024-02-04

    #include

    #include

    void main()

    int i,j;

    for(i=1;i<=100;i++)

    for(j=2;j<=sqrt(i);j++)if(i%j==0) If i is divisible by j, i is not prime, jump to lop.

    goto lop;

    printf("%d",i);

    lop:;

  4. Anonymous users2024-02-03

    Start by opening CodeBlocks and create a new project. Project language, select "c", you can write a program to determine the prime number, the specific method is:

    1. After creating the project, we open the "file".

    2. The first is to implement the input port.

    3. After the input port is completed, the next step is to judge the prime number.

    4. First define the relevant variables, here result is used to store the sum of factors, and use a loop to iterate through all possible factors.

    5. After the for loop is executed, what is stored in the result is the sum of the factors of prime, including itself. The next step is to determine if prime+1 is equal to result. If it is equal, it means that the result is 1+ the number itself, that is, the prime number.

    6. Compile and run it to see the result.

    Notes:

    Prime numbers are used in cryptography, the so-called public key is to add prime numbers to the information to be transmitted when encoding, and then transmit it to the recipient, after receiving this information, if there is no key owned by the recipient, then the process of decryption (in fact, the process of finding prime numbers) will be too long because of the process of finding prime numbers (decomposing prime factors), so that even if the information is obtained, it will be meaningless.

Related questions
16 answers2024-02-28

Definition of a for loop.

for(the initial value of the variable; termination of operating conditions; The initial value of the first cycle i is 0, and it runs until i=3 stops i=2, and the condition is still satisfied, and the loop body is still executed, and the step size is 1+1 each time >>>More

10 answers2024-02-28

main() [main function main program].

int i,j,k;【Define integer data i,j,k】for(i=1; i<=6;i++) main loop, i from 1 to 6, increasing to 1] for(j=1; j<=20-2*i;j++) subcycle, j from 1 to 20-2*i >>>More

12 answers2024-02-28

This problem requires understanding that the system allocates memory to static variables when compiling, and the memory units occupied by them are not released after the function call ends, that is, the value of the variable is the value of the previous function call at the next function call. >>>More

5 answers2024-02-28

Consider putting these 100 numbers in an array first, taking a random position one at a time (1-100 for the first time, 1-99 for the second time,..).Swapping the number of that position with the number of the last position will do the trick. >>>More

4 answers2024-02-28

I want to write it to you! It's a pity that I'm off work, hehe.