C language brush questions notes - print prime numbers

insert image description here

Article directory

topic

Determine how many prime numbers there are between 101-200, and output all prime numbers.
insert image description here

ideas

The method of judging the prime number : use a number to remove the number from 2 to the root, if it can be divided evenly, it means that the number is not a prime number, otherwise it is a prime number.

answer


#include <stdio.h>
#include <math.h>

int main()
{
    
    
    int m,i,k,h=0,leap=1;

    printf("\n");

    for(m=101;m<=200;m++)
    {
    
    
         k=sqrt(m+1);
         for(i=2;i<=k;i++)
         {
    
    
            if(m%i==0)
            {
    
    
                leap=0;
                break;
            }

            if(leap)
            {
    
    
                printf("%-4d",m);h++;

                if(h%10==0)
                {
    
    
                    printf("\n");
                }
            }

            leap=1;
         }
    }


    printf("\n\nThe total is %d",h);
}


Sample output

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/qq_21484461/article/details/124023574