C language learning: judgment of composite numbers and prime numbers

Prime and composite

Given a positive integer n (2 <= n <10000), determine whether it is a prime number.

Multiple input, one n per line (2 <= n <= 10000).

For each group of numbers, output Yes if it is a prime number, otherwise output No.

The use of loops: while (scanf(" ",& )!=EOF) multiple input loops, for loops;

Code:

#include <stdio.h>

int main()    //   注意:int t=0必须放在while 循环的里面  不然如果判断过一次t==1,t的初始

{                   //  值就会变成1而不再是0此后无论输入的数是质数还是合数输出的t值

   int a,n;        //  永远是1   会直接影响判断的结果。

   while(scanf("%d",&a)!=EOF)

   {

   int t=0;

   for(n=2;a>=n+1;n++)

   {

       if(a%n==0)

           t=1;

   }

   if(t==1)

   printf("No\n");

   if(t==0)

   printf("Yes\n");

   }

   return 0;

}      


In addition, if you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster! I may be able to help you here~

UP has uploaded some video tutorials on learning C/C++ programming on the homepage. Those who are interested or are learning must go and take a look! It will be helpful to you~

Sharing (source code, actual project video, project notes, basic introductory tutorial)

Welcome partners who change careers and learn programming, use more information to learn and grow faster than thinking about it yourself!

Programming learning:

Programming learning:

Guess you like

Origin blog.csdn.net/weixin_45713725/article/details/114698745