Reputation: 13
For instance, where a number = 0 or 1, it outputs:
"It's neither prime nor composite" //Desired
"0/1 is a prime number." //Not desired
For another instance, where a number is a negative number (e.g. -1):
"Error! You have entered a negative number." //Desired
"-1 is a prime number." //Not desired
How do I go about excluding those not desired outputs? I suspect the for loop causing those numbers(0,1,-1) to return 1, resulting in those outputs to appear. Any suggestions and hints would be great. Thanks!
Here's my implementation:
#include <stdio.h>
/* Checks if a positive number is a prime number. */
int is_prime(int num) {
/* Divisible by any integers from 2 to a num's square root */
for (int j=2; j<=num/2; j++) {
/* return "false" */
if (num%2 == 0) {
return 0;
}
}
/* Otherwise, return "true" */
return 1;
}
int main() {
int number;
printf("Enter a positive integer:\n");
scanf("%d", &number);
if (number == 0 || number == 1) {
printf("It's neither prime nor composite.\n");
} else if (number < 0) {
printf("Error! You have entered a negative number.\n");
}
is_prime(number);
if (is_prime(number) == 1) {
printf("%d is a prime number.\n", number);
} else if (is_prime(number) == 0) {
printf("%d is not a prime number.\n", number);
}
return 0;
}
Upvotes: 0
Views: 103
Reputation: 24052
Just change main
as follows:
int main() {
int number;
printf("Enter a positive integer:\n");
scanf("%d", &number);
if (number == 0 || number == 1) {
printf("It's neither prime nor composite.\n");
} else if (number < 0) {
printf("Error! You have entered a negative number.\n");
} else if (is_prime(number)) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}
return 0;
}
This way it doesn't call is_prime
if one of the special cases is in effect. I also removed some unnecessary calls to is_prime
(the first call did nothing at all, and the last call was redundant).
Update: I should point out that this post only addresses the problems in main
. When I posted this, I had not looked at is_prime
. See coderredoc's post for how to fix it.
Upvotes: 1
Reputation: 30926
The second line is also desired behavior.
Look at your code...
if (number == 0 || number == 1) {
printf("It's neither prime nor composite.\n");
return 0;
} else if (number < 0) {
printf("Error! You have entered a negative number.\n");
return 0;
}
Because if you don't do this or something similar then it calls is_prime
which returns 1 and prints the second line.
For 0,1,-1
the for
loop in is_prime
method is not run. And in the last line of the method you return 1
. As a result printf
prints that it is prime.
In your prime detection code..you should use this if (num%j == 0)
. otherwise it is an error.
int is_prime(int num) {
/* Divisible by any integers from 2 to a num's square root */
for (int j=2; j*j<=num; j++) {
/* return "false" */
if (num%j == 0) {
return 0;
}
}
/* Otherwise, return "true" */
return 1;
}
In your code you have unnecessariily called the is_prime
method twice. One of them was not even considering the returned value.
Also sometimes you will have to call a method and whose result will decide which one you need to output. Then instead of if-else
you can store the return value and then work on it.
int retVal = func(); if(retVal==1) printf(".."); else if( retVal==2) printf("..something different.."); else printf("..something else..");
When it is just two values or two types you are returning you can do this
if(func()==2)
// do this
else
// do that
Upvotes: 2