Problem #38
Solution Source Code
Code Overview
1. Header Files and Namespace:
<iostream>
is included for input and output operations.<string>
is included for potential future use.<cmath>
is included to use round() for calculations.using namespace std;
allows the use of standard functions without prefixing them withstd::
.
2. Enumeration (enPrimNotPrime
)
- Defines two values:
Prime = 1
(for prime numbers).NotPrime = 2
(for non-prime numbers).
3. User Input Function (ReadPositiveNumber
)
- Uses a do-while loop to repeatedly prompt the user for a positive number.
- Ensures the input is greater than 0.
- Returns the validated integer.
4. Prime Check Function (CheckPrime
)
- Takes an integer as input.
- If the number is less than 2, it is not prime.
- Uses half-range optimization to check for divisibility only up to
N/2
. - Returns:
enPrimNotPrime::Prime
if the number is prime.enPrimNotPrime::NotPrime
if the number is not prime.
5. Output Function (PrintNumberType
)
- Calls
CheckPrime()
to check if the number is prime. - Uses a switch statement to print:
"The number is Prime"
if prime."The number is Not Prime"
if not prime.
6. Program Execution (main()
)
- Calls
ReadPositiveNumber()
to get user input. - Calls
PrintNumberType()
to determine and print primality. - Returns
0
to indicate successful execution.
This structured explanation ensures clarity and ease of understanding.
0 comments