Hey i guuys, i researched for this topic and there were some close titles to this but none exactly helped me.

I need a write a boolean function named IsPrime that takes and integer as argument and returns true if it is. I have to demonstrate it in a full program. I know i have to use the % operator but im still kinda lost.

I was thinking using the % operator to see if there is a remainder, and if it does then return false.

Im really new to this language and my professor is going really quick. Please help

Recommended Answers

All 7 Replies

What do you need help with? Maybe this will get you started.

#include <iostream>

using namespace std;

bool isPrime(int number){
  bool isPrimeNumber = false;
  //do logic here

  return isPrimeNumber;
}

int main(){
  cout << boolalpha << "Is 5 prime : " << isPrime(5) << endl; //should be true
  cout << boolalpha << "Is 97 prime: " << isPrime(97) << endl; //should be true
  cout << boolalpha << "Is 27 prime: " << isPrime(27) << endl; //should be false

 return 0;
}

What do you need help with? Maybe this will get you started.

#include <iostream>

using namespace std;

bool isPrime(int number){
  bool isPrimeNumber = false;
  //do logic here

  return isPrimeNumber;
}

int main(){
  cout << boolalpha << "Is 5 prime : " << isPrime(5) << endl; //should be true
  cout << boolalpha << "Is 97 prime: " << isPrime(97) << endl; //should be true
  cout << boolalpha << "Is 27 prime: " << isPrime(27) << endl; //should be false

 return 0;
}

Hey i guuys, i researched for this topic and there were some close titles to this but none exactly helped me.

I helped with (or at least followed) many of the prime number programs here. If memory serves, and it does, almost all will help you.

You have basically 2 things to do:
1) test a value for prime. Reread a few of those unhelpful threads and you'll learn how to figure out if a number is prime or not.
2) take that code and turn it into your function.

try researching this site

Edit
See WaltP

thank you guys. I have been trying with what you told me. Thanks for the code, it give me a heads up to start. This is what i got and im still receiving an error.

#include <iostream>
#include <iomanip>


using namespace std;

bool isPrime(int number){
  int remainder;
  bool isPrimeNumber = false;
  cout<<"Please enter a number"<<endl;
  cin>>number;
  remainder = number % number;
      if (remainder=0){
          bool isPrimeNumber = true;
      }
      else {

      }

  //do logic here

  return isPrimeNumber;
}

int main(){
  cout << boolalpha << "Is 5 prime : " << isPrime(number) << endl; 

  system("pause");

 return 0;
}

Thanks

Still receives an error? We didn't know you had an error in the first place?

Your really need to learn to ask for help properly. The info you gave so far has been totally incomplete -- meaning we can't tell what's happening because you didn't tell us.

You need to read this, especially #1, #2, #4, #5:

[boilerplate_help_info]

Posting requests for help must be well thought out if you want help quickly and correctly.  Your post did not meet the criteria for quality help. You may get some posts, but are they going to be useful?  Check your post with these checkpoints - what is it [i]you[/i] missed:
[list=1]
[*]Ask a question that can be answered. Do not ask
- What's wrong with my code?
- Why doesn't this work?
- Anything else that does not give us useful information.
[*]Post your code.  If we don't know what you did, how can we possibly help?
- Use [b]PROPER FORMATTING[/b] -- see this
- Use CODE Tags so your formatting is preserved.
If we can't follow your code, it's difficult to help. We don't care that you're still working on it. If you want us to read it, it must be readable
[*]Explain what the code is supposed to do.  If we don't know where the target is, how can we help you hit it?
[*]Explain what actually happened! If we don't know where the arrow went when you shot it, how can we tell what went wrong and how far from the target you are?
[*]If you have errors, post them! We can't see your screen.  We can't read your mind. You need to tell us what happened.
[*]Do [b]not[/b] ask for code. We are not a coding service. We will help you fix your code. 
    If anyone posts working code for you, they are a cheater. 
    If you use that code [i]you[/i] are a cheater.
[*]Do [b]not[/b] bore us with how new you are. We can tell by your code.
- Do not apologize. We were all new, and unless you are completely 
  brain dead you will get better.
- Do not ask us to "take it easy on you."
- Do not say "I don't know what's going on." That's obvious since
  you posted for help. Use that time wisely by [b]explaining[/b] as best 
  you can so we can help.
[*]Do not apologize for posting 'late'. We don't have any expectations on when you should be posting - 10 minutes or 10 days. We aren't timing your responses.
[*][b]Do not post your requirements and nothing else. [/b]We view that as a lazy do-nothing student that wants us to do their work for them. That's cheating and we [i]will[/i] be hard on you.
[*]Do not attach files except when absolutely necessary. Most of us are not going to download files.  Add the information to your post.
[*][b]Do not tell us how urgent it is.[/b] Seriously, for us there is no urgency at all. Many that can help will ignore any URGENT or ASAP requests.
[*]Create a [b][i]good[/i][/b] title for your post. The title [b]C++[/b] in the C++ forum is bloody redundant and worthless!  [b]What's wrong?[/b] equally so. Specifically what are you having trouble with? [i]There[/i] is your title. [i](note: [b]my program[/b] is not the answer.)[/i]
[/list]
Think more about your next post so we don't have to play 20 questions to get the info we need to help you.

[/boilerplate_help_info]

Why do you have your number variable checking for a remainder on itself? Any number divided by itself is going to have no remainder, so your check is always going to come back true.

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.