-
#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);
-
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;
-
#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:;
-
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.
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
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
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
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
I want to write it to you! It's a pity that I'm off work, hehe.