Listing Prime Numbers Between 1 and N using loops

Page 1 of 1

2 Replies - 21900 Views - Last Post: 08 July 2006 - 03:57 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=17104&amp;s=9a5378f8030a7db7659ae09931568af4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 dhice

Reputation: 0
• Posts: 3
• Joined: 07-July 06

Listing Prime Numbers Between 1 and N using loops

Posted 07 July 2006 - 05:41 PM

hello all new to site,

My homework assignment is to:

Project 6 Description:

Quote

This program requires the utilization of concepts in Chapter 5 and Chapter 6. This program uses
two methods to determine the prime numbers that exist between 2 and a maximum value provided by the
user. The prime numbers along with some other information is written to an output file specified by the
user.
The program is to run in a loop controlled by the maximum value entered by the user. As long as
the value is greater than or equal to 2, the program executes. For values less than 2, the program
terminates. Each time through the program, the user enters the name for an output file. Run the sample
solution to understand how the program is to operate
The overall construct of the program is an outer loop that is controlled by the maximum value
entered by the user (use a while loop). Inside this loop, the output file is prompted for and opened, and
the two methods for determining the prime numbers are run. Each method requires 2 for loops (for loops
are much better suited for this part of the problem than are while loops). The outer loop runs through all
the possible numbers to test, and the inner loop performs the test on the outer loop numbers.
For each of the two methods used to determine the prime numbers, a loop iteration counter, that
counts how many times the loops are executed, and an event counter, that counts the number of prime
numbers found, are needed. The prime numbers are written to the output file as they are found. Use a
field width of 5 and left justification when printing out the prime numbers. Each time the outer loop OR
the inner loop is executed, the iteration counter is to be incremented.
Since two methods are going to be compared, each method should have its own variables for the
iteration counter and event counter.

and here is the code i have thus far:

```//	   		 Lab Section 01					   #
//			   07/06/06					   #
//	This Program Will Determine All Prime			 #
//	Numbers Between 2 And A Given Number				  #
//\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

int main ()

{
int input;
bool Prime = true;
string filename;

cout << "Enter In A Number To Be Tested: " << endl;
cin >> input;
cout << "Enter The Name Of The Output File: " << endl;
cin >> filename;

//###################### FIRST METHOD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
while( input >= 2)
{
ofstream P6;
P6.open(filename.c_str());

for(int counter = 2; counter <= input; counter++)
{
for(int counter2 = 2; counter2 <=input && Prime == true; counter2++)
{
if(((counter%counter2)==0) & counter2!=1 & counter2!=counter)
{
Prime = false;
break;
}
else
{

cout << counter << endl;
}
}
}

cout << "Please Enter A Number Less Than 2: " << endl;
cin >> input;
}

return 0;
}

```

// the cout is there just for me to view my output instantly (my university uses Solaris)

but here are my problems
• Prime Numbers Duplicate Themselves A Number Of Times
• Multiple's of 5 exist
• cannot use functions or classes

Is This A Good Question/Topic? 0

Replies To: Listing Prime Numbers Between 1 and N using loops

#2 dhice

Reputation: 0
• Posts: 3
• Joined: 07-July 06

Re: Listing Prime Numbers Between 1 and N using loops

Posted 07 July 2006 - 06:18 PM

changed a few things:

```//**************************************************
//									   #
//			  Dustin Hice					   #
//	   		 Lab Section 01					   #
//			   07/06/06					   #
//	This Program Will Determine All Prime			 #
//	Numbers Between 2 And A Given Number				  #
//\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

int main ()

{
int input;
bool Prime = true;
string filename;

cout << "Enter In A Number To Be Tested: " << endl;
cin >> input;
cout << "Enter The Name Of The Output File: " << endl;
cin >> filename;

//###################### FIRST METHOD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
while( input >= 2)
{
ofstream P6;
P6.open(filename.c_str());

for(int counter = 2; counter <= input; counter++)
{
for(int counter2 = 2; counter2 <= input; counter2++)
{
if(counter % counter2 == 0)
{
break;

}
else
{

cout << counter << endl;
}
}
}

cout << "Please Enter A Number Less Than 2: " << endl;
cin >> input;
}

return 0;
}

```

When I tested this out with 9 and 6 here are my results:

3
5
5
5 // when i entered 9
7
7
7
7
7
9

3
5 // when i enter 6
5
5

This post has been edited by dhice: 07 July 2006 - 06:21 PM

#3 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 175
• Posts: 4,667
• Joined: 26-November 04

Re: Listing Prime Numbers Between 1 and N using loops

Posted 08 July 2006 - 03:57 AM

Just add the break statement after the cout statement like this:
```else
{
cout << counter << endl;
break;
}

```

Once you've found a prime number, you don't want the nested loop to run again for the same number...do you?