Hey everyone,
I hve just recently started learning c++ and i have to write a program in which a user enters a number and that program tell that user if it is a prime number or not and if its not a prime number then it tell him all of its divisors and it out put divisors 5 per line only.
I have not learned arrays and stuff and so this program can only be written using while and if and for kind of stuff nothing more than that.
Im just confused on finding a prime number and out putting all the divisors can someone please help me out with this program ???
For this you don't actually need to find any prime numbers, you just need to test f a number is prime. Think about what makes a number prime: if you divide by every integer between 2 and sqrt(n) and they all have remainders, then the number is prime. So really you just need to loop from 2 to sqrt(n) and if the remainder of division of n by the loop index is 0, it is not a prime number. If you get through the whole loop then it is prime. Work on that first - it will help you to understand the factors part better.
Yes, but you should work on the core of the code first. Reread my post.
LB wrote:
you just need to loop from 2 to sqrt(n) and if the remainder of division of n by the loop index is 0, it is not a prime number. If you get through the whole loop then it is prime.