Thread: Print which numbers that are prime number from 2-100? Logical Error.

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Print which numbers that are prime number from 2-100? Logical Error.

    My code is supposed to check from starting at number 2 if it's a prime number and which others that are prime numbers up to 100. If it is a prime number the program should print "Prime" and if it's not a prime number the program should print "Num".

    I've got one for loop that checks if the number is a prime number. It works fine for the first one which is 2. But all the numbers after 2 is according to my program a number and not a prime number.

    I got an int variable called "check" which has the value of "n".

    Code:
    int check = n;
    At the end of the outer for-loop I increase "n" (n++) and since I'm initializing the "check" variabel in the beginning of this for-loop shouldn't the n-value change there too?

    Is it the "check" variable in the inner for-loop that doesn't change it's value?

    Am I even on the right track to solving the problem or should I try antoher solution? Or is my solution possible to make correct?

    Code:
    #include <iostream>
    
    
    using namespace std; 
    
    
    int main() {
    	
    	bool is_prime = true; 
    	int n = 2;  
    	int range = 100;
    	
    	// Kolla hur många tal som är primtal 2-100
    	for (int j = 0; j < range; j++) {
    		
    	int check = n;
    	
    		// Checks if a number is a prime
    		for (int i = 2; i < check; i++) {
    		
    			if (n % i != 0) {
    			
    			}
    			else {
    				is_prime = false; 
    			}
    		}
    	
    			if (is_prime && n > 1) {
    				cout << "Prime: " << n << endl;
    			}
    			else {
    				cout << "Num: " << n << endl;
    			}
    		
    		n++;
    	}
    	
    	return 0; 
    }

  2. #2
    Guest
    Guest
    On line 9 you set is_prime to true, but once you enter the outer loop the only operation you permit is setting it to false, so it doesn't get another chance to be true again.

    You code indentation, while not bad, could see improvement.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    This solution seems to work very well . Thanks for your clue Adrian. And I should put the code block
    in a function instead of having it in main by the way.

    Code:
    #include <iostream>
    
    
    using namespace std; 
    
    
    int main() {
    	
    	bool is_prime = true; 
    	
    	// Initialize variables n and range to avoid unexpected behavior
    	int n = 2;  
    	int range = 0;
    	
    	cout << "Start at: "; 
    	cin >> n; 
    	
    	cout << "Stop at: "; 
    	cin >> range; 
    	
    	cout << endl; 
    	
    	// Check how many numbers from range x - y 
    	// that are prime numbers
    	for (int j = n; j <= range; j++) {
    	
    	// Prime must always be true to be able to
    	// change to false. 
    	is_prime = true; 
    	
    		// Checks if a number is a prime or not
    		for (int i = 2; i < n; i++) {
    		
    			if (n % i != 0) {
    				continue; 
    			}
    			else {
    				is_prime = false; 
    			}
    			
    		}
    		
    		// Print if it is a prime number or a number
    		if (is_prime && n > 1) {
    			cout << "Prime number: " << n << endl;
    		}
    		else {
    			cout << "Number: " << n << endl;
    		}
    		n++;
    	}
    	
    	return 0; 
    }

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I'm glad that the trial division version is apparently working, but implementing the sieve of Eratosthenes would have given you all of the primes within [1,100] too. I prefer it for assignments like these.

  5. #5
    Guest
    Guest
    Glad you got it to work.

    And I should put the code block
    in a function instead of having it in main by the way.
    Yes, creating a bool isPrime(int); function would be a good exercise.

    Instead of this:
    Code:
    if (n % i != 0) {
        continue; 
    }
    else {
        is_prime = false; 
    }
    Consider:
    Code:
    if (n % i == 0)
        is_prime = false;
    ...or add a break statement to the else bracket, if that was the intention.

    Also, I don't think your program will ever encounter n <= 1, so there's no need to check for it when printing the numbers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime Number Generator also Printing Composite Numbers
    By Osman Zakir in forum C++ Programming
    Replies: 3
    Last Post: 04-17-2015, 05:16 PM
  2. Replies: 3
    Last Post: 12-02-2014, 10:11 AM
  3. Logical error in Armstrong number code
    By dumb09 in forum C Programming
    Replies: 5
    Last Post: 10-21-2014, 05:18 PM
  4. Replies: 1
    Last Post: 11-04-2011, 01:16 PM
  5. Hi, Can anyone help me? print prime numbers.
    By kelai in forum C Programming
    Replies: 3
    Last Post: 09-29-2010, 01:35 PM