2014-10-17 2 views
-6

안녕하세요 여러분, 제 프로그램은 10-200에서 소수를 찾을 수있는 프로그램입니다.소수 만 10 ~ 200에서 while 회 돌이 만 사용 C++

#include <iostream> 
using namespace std; 
void main() 
{ 
    int n=10 , x=2; 
    bool t=false; 

    while(n<=200) 
    { 
     while(x<n-1) 
     { 
      if(n%x!=0) 
      { 
       x++; 
       t=true; 
      } 
     } 
     if(t==true) 
     { 
      cout<<n <<endl; 
      n++; 
     } 
    } 
} 
+4

거의 모든 것. –

+1

마음에 오는 첫 번째 일들 - 들여 쓰기. 팁이 있습니다. 사람들이 당신을 도우려는 경우, 대답하기 쉬운 질문을하기 위해 시간을 할애해야합니다. –

+0

글쎄, 미안하지만, 여기 처음으로 나는 루프에 익숙하지 않다. 나는 그냥 잡았다. –

답변

1

코드에 많은 실수가 있습니다. 나는 당신의 논리를 이해하지 못하지만, 당신은 이것을 할 수 있습니다 :

#include <iostream> 
using namespace std; 

int main() 
{ 
    // checking number starting from 10 
    int n = 10; 
    bool isPrime; 

    // as long as the number is less than or equal to 200 
    while(n <= 200) { 

     // assume the number is prime 
     isPrime = true; 

     // since prime number is the number that can only be divided by 1 and itself, 
     // check if this number can be divided by any number other than 1 or itself 
     for (int i = 2; i < n; i++) { 
      // if this number can be divided by any number other than 1 or itself, 
      if (n%i == 0) { 
       // then this is not a prime number, no need to check anymore 
       isPrime = false; 
       break; 
      } 
     } 

     if (isPrime == true) { 
      cout << n << endl; 
     } 

     // check the next number 
     n++; 
    } 
} 
+0

젠장, 고마워, 너는 나를 도왔던 유일한 사람이야! –

+0

당신을 진심으로 환영합니다! 내가 도와 줘서 기뻐. – Edwin