开发者

How to write a program that shows if a number is prime and can display the prime numbers from 1 to 100 in a file

I am trying to write a program that will tell you if the numbered entered is prime or not and will write all the prime numbers from 1 to 100 to a file and displays the numbers. This is what I have so far, but I'm lost.

bool isPrime(int);

int _tmain(int argc, _TCHAR* argv[])
{
    int num, answer, choice, i, numb=1;

    do
    {
        cout<< "Enter a number and I will tell you if it is prime or not."<<endl;
        cin>> num;

        if (isPrime(num))
            cout<<num<<" is a prime number."<<endl;
        else 
            cout<<num<< " is not a prime number."<<endl;

        cout<<"Would you like the first 100 prime numbers displayed? (1 for yes and 2 for no)"<<endl;
        cin>>choice;

        if (choice == 1)
        {
            while(numb<=100)
            { 
                i=2; 

                while(i<=numb)
                { 
                    if(num%i==0)
                        break;

                    i++;
                }

                if(i==num)
                    cout<<numb<<" is Prime"<<endl;

                numb++;
            }
        }
        else
        {
            cout<<"Would you like to run the program again? (1 for yes and 2 for no)"<<endl;
            cin>>answer;

            if (answer == 2)
            {
                exit(0);
            }
        }
        while (answer == 1);
    }

    system("pause");

    retur开发者_StackOverflow中文版n 0;
}

bool isPrime (int number)
{
    int i;

    for (i=2; i<number; i++)
    {
        if (number % i == 0)
        {
            return false;
        }
    }

    return true;    
}


Really feel you are over-thinking this. You've done the hard part which was writing the isprime function.

Displaying the numbers is trivial, just write a for loop to go through the numbers and check which are prime, if a specific number is prime then print it to screen.

Then just add the write to file inside the loop for those numbers you print to screen.


Why not just reuse your isPrime()?

cout<<"Would you like the first 100 prime numbers displayed? (1 for yes and 2 for no) <<endl;

cin>>choice;

for (i=2; i < 100; i++)
{
  if (isPrime(i)) cout << i << endl;
}


You're way over complicating things for yourself when printing all the primes from 1 to 100. Take a step back and think about what you want to do; cycle from 1 to 100, print the number if its prime.

for (int i = 1; i <= 100; ++i) {
  if (isPrime(i))
  cout << i << endl;
}


The while keyword of your do-while loop is on the wrong line. It should follow the closing brace. Compiler said around line 56 of the example code you posted.

After making changes to conform to standard C++, I compiled and ran the program. I chose the option to list all the primes up to 100. It is generous and displaying all the numbers, prime or not (hint: even numbers after 2 are not prime).

I inserted the following lines at the beginning:

#include <iostream>
using namespace std;

I changed the main function from _tmain to main since I'm not using Visual Studio compiler. Likewise the arguments too:

int main(int argc, char * argv[])

By the way, if you are not passing parameters to your program, you can simplify the declaration of main to:

int main(void)

Here is a modification to speed up your prime detector:

bool isPrime (int number)
{
    int i;

    if (number == 2)
    {
        return true;
    }
    if ((number % 2) == 0)
    {
        return false;
    }
    for (i = 3; i < number; i += 2)
    {
        if (number % i == 0)
        {
            return false;
        }
    }

    return true;    
}

This cuts down the number of checks by half because every even number after 2 is not prime, only odd numbers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜