Hello. I have to do an program that writes an scale from num 1 to num 2. I managed to do that writes out from 1 to given number. Can anyone help me do that I can insert an scale starting number?

Code:

#include <iostream>

using namespace std;

void prime_num(int);

int main(){
    
    cout << " Enter end of the scale: ";
    int num = 0;
    cin >> num;
    prime_num(num);
}

void prime_num( int num){
    
    bool isPrime=true;
    
    for ( int i = 0; i <= num; i++) {
        
        for ( int j = 2; j <= num; j++){
            if ( i!=j && i % j == 0 ){
                
                isPrime=false;
                break;
            }
        }
        
        if (isPrime){
            
            cout << i << endl;
        }
        isPrime=true;
    }
	return 0;
}

Thanks in advance

Recommended Answers

All 13 Replies

If I understand your question, you want to establish a starting value as well as an ending value for your prime number search?

If so, that's quite simple; instead of having a single num, have two ints: start_nbr and end_nbr. Prompt the user for them separately, and pass them both to your prime number generator.

Or, are you trying to do something more here?

yeah thats pretty much it, but I'm pretty sure that i have to do something with functions... for example... if you put starting number 5 and ending number 15 it would have to write out every prime number between 5 and 15. And I can't figure it out. Thanks in advance.

This seems to work for me:

int main(){
	int startNbr, endNbr;

	cout << " Enter start of the scale: ";
	cin >> startNbr;

	cout << " Enter end of the scale: ";
	cin >> endNbr;

	prime_num(startNbr, endNbr);
}

void prime_num(int startNbr, int endNbr){

	bool isPrime=true;

	for ( int i = startNbr; i <= endNbr; i++) {

		for ( int j = 2; j <= endNbr; j++){
			if ( i!=j && i % j == 0 ){

				isPrime=false;
				break;
			}
		}

		if (isPrime)
			cout << i << endl;

		isPrime=true;
	}
}

Yeah thanks! Thats perfect. Just one more thing if you have time, i only need one more thing that is: Count how many prime numbers that function returns. thanks

Tell you what: I'll outline it, but you have to do the coding, OK?

You'll want a static integer in your prime_num routine, initialized to 0. You'll use it as a counter. Every time you test true for a prime, you increment the counter. At the end of the routine, you print out the value.

I tried like this:

#include <iostream>

using namespace std;

int number = 0;
void prime_num(int startNbr, int endNbr);

int main(){
	int startNbr, endNbr;
    
	cout << " Enter start of the scale: ";
	cin >> startNbr;
    
	cout << " Enter end of the scale: ";
	cin >> endNbr;
    
	prime_num(startNbr, endNbr);
    cout << number << endl;

}

void prime_num(int startNbr, int endNbr){
    
	bool isPrime=true;
    int p = 0;
	for ( int i = startNbr; i <= endNbr; i++) {
        
		for ( int j = 2; j <= endNbr; j++){
            
			if ( i!=j && i % j == 0 ){
                
                
				isPrime=false;
				break;
			}
            
		}
		if (isPrime)
            number=p++;
			cout << i << endl;
            isPrime=true;
        
	}
}

but as soon as i insert p++ the output is:

Enter start of the scale: 10
 Enter end of the scale: 20
10
11
12
13
14
15
16
17
18
19
20
3

Take a closer look at your new code. Make sure you have braces everywhere you want them.

Proper indentation helps you find bugs faster, too.

Im sorry i know its annoying to help someone at amateur problem like this, but i cannot figure it out.

It's not annoying; I'm just trying to help you find the problem, rather than simply give you the answer.

By not using braces after the "if" on line 38, you've removed your cout from the if test. It will print every time.

Just increment the prime counter here:

if (isPrime){
 
            cout << i << endl;
        }

and then just printf() it. (in the end of the function).
Your code didnt work because you deleted all the braces, making the code display a number between 10 and 20 all the time, but only incrementing the prime conter when the number is prime indeed, thats why you have a 3 number after the 20.

thank you i did it. Here it is if anyone else will need it:

if (isPrime)
        {
            number=p++;
			cout << i << endl;
        }
            isPrime=true;

would be nice if you mark the thread as solved and rate my post =p

Done. Thanks everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.