Menu Close

找出出两个正整数之间的质数并打印出来

#include <stdio.h>
int main()
{
   int num1, num2, flag_var, i, j;
 
   /* Ask user to input the from/to range
    * like 1 to 100, 10 to 1000 etc.
    */
   printf("Enter two range(input integer numbers only):");
   //Store the range in variables using scanf
   scanf("%d %d", &num1, &num2);
 
   //Display prime numbers for input range
   printf("Prime numbers from %d and %d are:\n", num1, num2);
   for(i=num1+1; i<num2; ++i)
   {
      flag_var=0;
      for(j=2; j<=i/2; ++j)
      {
         if(i%j==0)
         {
            flag_var=1;
            break;
         }
      }
      if(flag_var==0)
         printf("%d\n",i);
  }
  return 0;
}
Enter two range(input integer numbers only):5 90
Prime numbers from 5 and 90 are:
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
READ  C语言字符串练习:输入两个字符串并把他们链接起来 (不能用strcat函数)
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Leave the field below empty!

Posted in C语言习题集

Related Posts