C++ || Find The Prime, Perfect & All Known Divisors Of A Number Using A For, While & Do/While Loop

Print Friendly, PDF & Email

This program was designed to better understand how to use different loops which are available in C++.

This program first asks the user to enter a non negative number. After it obtains a non negative integer from the user, the program will determine if the inputted number is a prime number or not, aswell as determine if the user inputted number is a perfect number or not. After it obtains its results, the program will display to the screen if the user inputted number is prime/perfect number or not. The program will also display a list of all the possible divisors of the user inputted number via cout.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Do/While Loop
While Loop
For Loop
Modulus
Basic Math - Prime Numbers
Basic Math - Perfect Numbers
Basic Math - Divisors


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output:

Enter a number: 8128

Input number: 8128
8128 is not a prime number.
8128 is a perfect number.
Divisors of 8128 are: 1, 2, 4, 8, 16, 32, 64, 127, 254, 508, 1016, 2032, 4064, and 8128

Do you want to input another number?(Y/N): y
------------------------------------------------------------
Enter a number: 6

Input number: 6
6 is not a prime number.
6 is a perfect number.
Divisors of 6 are: 1, 2, 3, and 6

Do you want to input another number?(Y/N): y
------------------------------------------------------------
Enter a number: 241

Input number: 241
241 is a prime number.
241 is not a perfect number.
Divisors of 241 are: 1, and 241

Do you want to input another number?(Y/N): y
------------------------------------------------------------
Enter a number: 2012

Input number: 2012
2012 is not a prime number.
2012 is not a perfect number.
Divisors of 2012 are: 1, 2, 4, 503, 1006, and 2012

Do you want to input another number?(Y/N): n
------------------------------------------------------------
Press any key to continue . . .

Was this article helpful?
👍 YesNo

Leave a Reply