Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    The Tired Prime Number Program, but different.

    Hi,

    I have figured out the old tired prime number program that a lot of newbies like myself attempt to tackle when we hit the ground running with c++. What I am interested in is expanding it to something that not only displays the primes of a number, but the number of primes. To wit:

    Please enter a number: 10

    There are 4 primes in 10. Those primes are:

    7 5 3 2

    Do you wish to continue (y/n)?

    I have all of that down except for the "there are x primes". I have tried count and if/else/do but I am getting hopelessly confused. Is there a simple or other way to do it?

    Here is my code so far...

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int x; // input
    	int y; // divisor
    	int z; // prime
    	
    	char key; // to continue or not
    
    	cout << "This program will allow you to enter a number and will display all and how many prime numbers from that number to 0." << endl << endl;
    
    
    do
    {
    	cout << "Please enter a number: ";
    	cin >> x;
    
    	cout << endl;
    	// cout << "There are " << counter << " primes in that range." << endl;
    	cout << "All primes in this range are:" << endl << endl;
    
    	for (z=x; z>=2; z--)
    	{
    		bool primeNo = false;
    
    	for (y=2; y<z; y++)
    	{
    	
    		if (z % y == 0)
    		{
    			primeNo = true;
    		}
    	}
    		if (primeNo == false)
    		{
    			
    			cout << z << " ";
    			
    		}
    	}
    
    	cout << endl << endl;
    	cout << "Continue (y/n)? ";
    	cin >> key;
    }
    
    	while (key == 'y'||key == 'Y');
    	cout << endl << endl;
    	
    	system("PAUSE");
    
    	return 0;
    
    }

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    That is easy to do. Just create a variable to keep track of the number of primes. Each time you determine a prime, increment the number.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well I'm not so used to C++ but only C. I can't help with the syntax but maybe a little bit with the logic.

    1. Try using an array to store all the primes ?
    Well as a number is found to be a prime it is stored in an array and later when all the primes have been found the loop terminates and you print the array. And for the count maybe a variable who's count increases as you find each prime ?

    2.Printing each Prime as it is found and having a variable increment along, finally as the loop ends. Printing the Increment to give the total number of primes.

    I haven't tried either of the concepts on C, I'll give it a try and incase it works, I'll bring it up here.


    Let me know if any of it works for you.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •