2019年6月26日 星期三

C語言(online c) 寫簡單的 程式 ( 輸入2個數值 印出2個數值間的質數及質數的個數 )

C語言(online c) 寫簡單的 程式 ( 輸入2個數值 印出2個數值間的質數及質數的個數 )

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

int main()
{
    int x,y,i,j ,cnt=0 ,cnt2=0;
    printf("輸入2個數值 由小到大\n");
    scanf("%d %d", &x,&y);
    printf("你輸入的2個數值=%d %d\n", x,y);
   
    for (i=x;i<=y;i++)
    {
        cnt=0;
        for (j=2;j<=i-1;j++)
        {
            if (i%j==0)
                cnt++;
        }
        if (cnt==0)       
        {
            printf("%d\t",i);
            cnt2++;
        }   
    }
   
    printf("\n\n%d到%d的質數共有%d",x,y,cnt2);
    return 0;
}       


Stdin Inputs...
5 37

輸入2個數值 由小到大
你輸入的2個數值=5 37
5 7 11 13 17 19 23 29 31 37

5到37的質數共有10

沒有留言:

張貼留言

WOKWI ESP32 LED Control , Node-Red MQTT SQLITE  

WOKWI ESP32 LED Control ,  Node-Red  MQTT SQLITE   const char broker[] = "test.mosquitto.org" ; //const char broker[] = "br...