It prompts the user to enter an integer N. It prints out if it is a prime or not. If not, it prints out all of its proper factors.

[sourcecode language=’cpp’]

/*******************************************************
* MYCPLUS Sample Code – http://www.mycplus.com *
* *
* This code is made available as a service to our *
* visitors and is provided strictly for the *
* purpose of illustration. *
* *
* Please direct all inquiries to saqib at mycplus.com *
*******************************************************/

#include

int main(void) {
int n,
lcv,
flag; /* flag initially is 1 and becomes 0 if we determine that n
is not a prime */

printf(“Enter value of N > “);
scanf(“%d”, &n);
for (lcv=2, flag=1; lcv <= (n / 2); lcv++) { if ((n % lcv) == 0) { if (flag) printf("The non-trivial factors of %d are: \n", n); flag = 0; printf("\t%d\n", lcv); } } if (flag) printf("%d is prime\n", n); }[/sourcecode]

0 Shares
Share
+1
Tweet
Share
Stumble