[C] prime1.c It prompts the user to enter an integer →→→→→进入此内容的聊天室

来自 Mature Peafowl, 2022-06-20, 写在 C, 查看 79 次.
URL http://www.code666.cn/view/b1bd3fb1
  1. /* prime1.c  It prompts the user to enter an integer N. It prints out
  2.  *           if it is a prime or not. If not, it prints out a factor of N.
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main(void) {
  8.   int n;
  9.   int i;
  10.   int flag;
  11.  
  12.   printf("Enter value of N > ");
  13.   scanf("%d", &n);
  14.   flag = 1;
  15.   for (i=2; (i<(n/2)) && flag; ) { /* May be we do not need to test
  16.                         values of i greater than the square root of n? */
  17.     if ((n % i) == 0) /* If true n is divisible by i */
  18.       flag = 0;
  19.     else
  20.       i++;
  21.   }
  22.  
  23.   if (flag)
  24.     printf("%d is prime\n", n);
  25.   else
  26.     printf("%d has %d as a factor\n", n, i);
  27.   return 0;
  28. }

回复 "prime1.c It prompts the user to enter an integer "

这儿你可以回复上面这条便签

captcha