Test Prime Numbers - Peter's Program

  • Thread starter Thread starter peterjaybee
  • Start date Start date
  • Tags Tags
    Prime Test
AI Thread Summary
The discussion focuses on a programming issue related to a prime number testing code written in C++. The primary problem identified is that the variable `is_prime` is set to true outside the loop, which causes it to retain its value after the first execution, leading to incorrect results for subsequent inputs. Additionally, the absence of curly braces `{ }` around the code blocks for the `if` statements is highlighted as a critical mistake, as it causes `is_prime = false` to execute every time the loop runs, rather than only when a factor is found. The conversation also touches on the importance of understanding block structures in C++ compared to Python's indentation rules, emphasizing that proper use of braces is essential for clarity and correctness in the code. Overall, the code needs adjustments to ensure accurate prime number testing and proper control flow.
peterjaybee
Messages
62
Reaction score
0
Hi,

I'm new to programming and have written the following code to test for prime-ness, but it doesn't seem to work except for n = 3.

I think it may have something to do with my goto statement. Can anyone see a way of avoiding this or any other errors with the program?

Code:
#include <iostream>
#include<math.h>

using namespace std;

int main() {
	int n, i, is_prime;
	
	//Assume # is prime until proven otherwise
	
	is_prime = true;
	loop:
	
	// get number
	
	cout << "Enter a number and press ENTER (0 to exit): ";
	cin >> n;
	
	if (n == 0) {
		
		return 0;
		
	}
	
	else {
	
		//test for prime-ness.
		
		i=2;
		
		while (i <= sqrt(static_cast<double>(n))) {
			
			if (n%i == 0)
			
				cout << i << " is a factor of " << n << endl;
				is_prime = false;
			
			i++;
		}
		
		if (is_prime)
			cout << "Number is Prime!" << endl;
			
		else 
			cout << "Number is not prime!" << endl;
			
	}
	
	goto loop;
}

Kind regards,

Peter
 
Technology news on Phys.org
Always use { } with if statements - then hunt down the lecturer/textbook writer that misses them out for single line if statements.

Then as a bonus appreciate the Python indentation rules
 
What are the python indentation rules
 
When you do
if ()
... this line
... another line

Which line does the if() statement apply to in c/c++?
In python, it's the indentation that controls the blocks, in c/c++ the indentation means nothing.
 
Last edited:
Well, it's not a terribly efficient way to check for being prime, but it looks like it should work (except for any number less than 2, which it will think is a prime number). A quick scan makes me think that one of your problems is that you set is_prime to true OUTSIDE of your loop. So it'll work the first time you run it, and will continue to work until you give it a non-prime number, in which case it will continually return false, no matter what.

But as mgb_phys points out, your bigger issue is that you should ALWAYS use { } around your code blocks, unless you've gained a knack for programming. As it stands, "is_prime = false" is performed EVERY time you're in your while loop, not just when n % i == 0. Hence, it works with 2 or 3, because it immediately falls out of the while loop (i = 2, and sqrt(3) and sqrt(2) are less than 2).

DaveE
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top