# Thread: c programming function. Finding next value

1. ## c programming function. Finding next value

I've written up some code to find out if a value entered is a prime number. I have now extended this piece of code by adding another function to find the next prime number. I'm having a lot of trouble getting it working, mainly (i think) because I either can't get it to stop once it has found the next one, or, it's just printing the value+1 as the next value (even though it might not necessarily be prime).

My code is as follows:

Code:
```#include <stdio.h>#include <stdlib.h>
#include <math.h>

int isperfect(int);
int nextperfect(int);

int
main(int argc, char **argv) {
int value, sum, nextvalue;
printf("Enter two values: ");

while(scanf("%d", &value) == 1){
if(value<0){
printf("Error, negative value!");
exit(EXIT_FAILURE);
}

sum = isperfect(value);
if (sum == value){
printf("The value %d is perfect.\n", value);
}
nextvalue = nextperfect(value);
printf("The next perfect value is %d.\n\n", nextvalue);
}
return 0;
}

int
isperfect(int value){
int divisor, sum = 0;

for(divisor=1; divisor<value; divisor++){
if(value%divisor == 0){
sum = sum+divisor;
printf("The divisor value is = %d, and the value is %d\n", divisor, value);
}
}

return sum;
}

int
nextperfect(int value){
int nextvalue, sum, nextfound;

for(nextvalue = value+1; nextfound == 0 ; nextvalue++){
sum = isperfect(nextvalue);
if (sum == nextvalue){
nextfound = 1;
}
}

return nextvalue;
}```

2. > int nextvalue, sum, nextfound;
...
> for(nextvalue = value+1; nextfound == 0 ; nextvalue++)

What is the initial value of nextfound?

3. Originally Posted by Tigertan
I've written up some code to find out if a value entered is a prime number.
No, you've written code to find out if a value is a perfect number.

Bye, Andreas

4. Originally Posted by AndiPersti
No, you've written code to find out if a value is a perfect number.

Bye, Andreas
Ahh sorry, yes I meant perfect number! I've been writing up all sorts of programs over the past few days!

Originally Posted by Salem
> int nextvalue, sum, nextfound;
...
> for(nextvalue = value+1; nextfound == 0 ; nextvalue++)

What is the initial value of nextfound?
Why do i need to initialize nextfound?? That's just my guard.. isn't it okay like that?

5. Local variables that have not been assigned a value, just have a random value. It's unlikely that nextfound would happen to be equal to 0.

Global int variables ARE assigned the value of zero, when they're created.