Home > Back-end >  How to calculate prime c language
How to calculate prime c language

Time:06-09

Int k=0;

Scanf_s (" % d ", & amp; N);

If (n==1)

K=1;

For (I=2; I & lt; N/2; I++) {

If I==0) (n % {

K=1;

break;

}

}

If (k==o)

Printf (" % d is a prime number ", n);

Else if (k==1)

Printf (" % d is not a prime ", n);
Explain to the string of code

CodePudding user response:

There I was not defined, and divided by two, and can't judge his is a prime number, with a number inside found wrong.

CodePudding user response:

Int k=0; \ \ the k is used to determine whether as a prime number, not to 1, while is 0

Scanf_s (" % d ", & amp; N); \ \ then enter you need to determine the number of n

If (n==1) \ \ input number is 1, is not a prime number, the k value assignment 1

K=1;

For (I=2; I & lt; N/2; I++) {\ \ cycle let the input of n and 2 n/2 floor, look to whether have remainder

If I (n %==0) {//if there is a number can be divided, the remainder (n % I) is equal to 1, then assigned to k=1, after the break for the end loop statement

K=1;

break;

}

}

If (k==o)//if the n in the process of judgment is not considered a prime number, the k is equal to zero, can judge the n is a prime number;

Printf (" % d is a prime number ", n);

Else if (k==1)//if k=1, n to is not a prime,

Printf (" % d is not a prime ", n);

CodePudding user response:

 # include 
#include


Int isprime (int num)
{
Int I, rt.

Rt=(int) SQRT (num);

For (I=2; I & lt;=rt; I++)
If (num % I==0)
return 0;
return 1;
}

Int main ()
{
int m, n;
Int CNT=0, TMP, I;

The scanf (" % d % d ", & amp; M, & amp; N);

If (m & gt; N) {
TMP=m;
M=n;
N=TMP;
}

For (I=m; I & lt;=n; I++) {
{if (isprime (I))
Printf (" % d \ t ", I);
Cnt++;
If (% CNT==0, 10)
Putchar (10);
}
}

return 0;
}

For your reference ~

CodePudding user response:


I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10581430.html
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10768339.html
  • Related