-
#include
using namespace std;
int main()
int j;
for(int i=2;i<100;i++) The first cycle is from 2 to 100 numbers.
for(j=2;j<=i/2;j++) The second loop is to determine whether the value of i is prime.
if(i%j==0) is not prime if it is divisible. Jump.
break;
if(j>i 2) to determine whether the above loop ends normally
-
Define a function to determine whether an incoming number is prime. Traverse through all odd numbers of 2 and less than 100 in the main function, pass them into the judgment function one by one, and output them if they are determined to be prime numbers according to the return value. Here are some examples:
#include ""//if the vc++,with this line.
#include ""
int prime(int n){ Determine whether n is prime.
int i;
if(n>2 &&n&1) |n<2) Even numbers less than 2 or greater than 2 are not prime.
return 0;
for(i=3; i*i<=n;i+=2) An odd number that is divisible by a number is not a prime number.
if(!(n%i))
return 0;
return 1;The rest is prime.
int main(void){
int i;
for(i=2;i<100;i!=2 ?i+=2 : i++) only examines 2 and other odd numbers.
if(prime(i)) printf("%d ",i);
printf("");
return 0;
-
The upstairs are too complicated, what age. Look at me, short and concise!
#includevoid main()
int i,n; i=2,n=2;
while(i<=100) within 100 ;
if(i==n) to check whether i n is consistent (the prime number has only 1 and its own divisor), is to print printf("%d",i);
i++;After making a number, i adds one, and the next number is carried out, and the factor n returns to 2, and the cycle is repeated n=2; }
-
#include
#include
using namespace std;int main()int n,m;
bool isprime;
cout<<"2 is a prime number"if(n%m == 0) If the remainder is 0, it is a sum number.
isprime=false;
break;
if (isprime) output if it is prime.
cout
-
Programming C to output all prime numbers up to 100 can be as follows:
int i = 1;Circular variables.
bool s;Indicates whether it is a prime number.
for (;i < 100; i++)
s = true;Suppose the current i is prime.
for (int j = 2; j < i; j++)
If i is divisible by itself and a number other than 1, then he is not prime.
if (i % j ==0)
s = false;
If it is prime, it is output.
if (s)
-
Problem solving ideas: first find out all the integers within 100 200, and then let these integers remainder for numbers other than 1 and itself, if there is a divisible number, it is not a prime number, otherwise it is a prime number.
Start by finding all the integers from 100 to 200, all of which are i
Use i to find the remainder of a number other than 1 and itself.
Correct**:
#include
int main()
int conut = 0;
int i = 0;
for(i=100; i<=200; i++)int j = 0;
for(j=2; jif(i%j == 0)break;
if(j==i)
conut++;
printf("%d ", i);
printf("");
printf("The number of primes is: %d", conut);
return 0;
The judgment condition of the second if statement should be j==i; Instead of i%j.
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
#include
#include >>>More
#include
using namespace std; >>>More
However, it is clear that this is problematic because variable a is not assigned at all. >>>More
Let's give you a method.,This method is set up.,You can change it to read.,If you can read the configuration, it's no problem, right? >>>More