Ask coding questions

← Back to all posts
How do I do this?
NZHacker808 (96)

I got a bit confused with this, I don't know how to do the prime thing, the one who helps me will get 5 cycles!

Answered by malvoliothegood (719) [earned 5 cycles]
View Answer
Commentshotnewtop
malvoliothegood (719)

Have you searched for an answer using a search engine? If you do you will find many examples. Give it a go and save your 5 cycles.

NZHacker808 (96)

@malvoliothegood I am not sure what is this called so I don't know what to search

malvoliothegood (719)

@NZHacker808 Try the following search term: "C++ finding prime numbers" - include the quote marks. The first result I get looks promising. Here is the URL that I get when using the DuckDuckGo search engine: https://duckduckgo.com/?t=ffab&q=%22C%2B%2B+finding+prime+numbers%22&ia=web

malvoliothegood (719)

@NZHacker808 Looks like you found a solution. A tip: your loop counter does not need to increment in value until it is the same as the number that you are testing. It only needs to go halfway. Think about why this is. Here is a fork of your program that encapsulates the prime finding code in a function: https://repl.it/@malvoliothegood/Prime-checker

NZHacker808 (96)

@malvoliothegood That is very helpful! Upvoted :)

malvoliothegood (719)

@NZHacker808 Thanks. By encapsulating the prime finding in a function your program can be more robust. For example you can test out any number of numbers using a for loop.