0

So after a day or two of trying i finally found out how to do this, my question is, is there any way to do this that is more efficient without using the prime function?

#include <iostream>
using namespace std;


      int main()
{
 std::string line;
 
 int i, p, x;
 
 for(i = 3; i <=100; i++) {
       
       for(p=2; p<i; p++)
       {
                if((i%p) == 0) x = 0;
                }
                if(x != 0) cout << i << " is prime\n";
                x = 1;
                }
       
       
        
    
 getline(cin,line);
 cin.get();
 
 return 0;
}
3
Contributors
4
Replies
6
Views
9 Years
Discussion Span
Last Post by WaltP
0

how should i improve my formatting, can you please give me an example... i just get lost of what has to be where when im writing

0

example. Note placement of braces and spacing. For more information read this by the author of the c++ language.

#include <iostream>
using namespace std;


int main()
{
    std::string line;
 
    int i, p, x;
 
    for(i = 3; i <=100; i++) 
    {     
       for(p=2; p<i; p++)
       {
            if((i%p) == 0) 
                x = 0;
       }
       if(x != 0) 
             cout << i << " is prime\n";
       x = 1;
    }
    getline(cin,line);
    cin.get();
    return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.