vendredi 6 mars 2015

Program that outputs twin prime numbers



I need to write a code that has you input a number and it checks that the given number is a twin prime with the prime number that follows it. Here is what I have so far:



int isPrime (int Value){
int Number;
for (Number=2; Number <= Value-1; Number++){
if (Value % Number ==0)
return 0;
else
return 1;
}
}
int nextPrime(int current){
while (1){
if (isPrime(current))
return current;
current++;
}
}
int main(){
int a;
int First = nextPrime (a);
int Second = nextPrime(First + 1);
scanf("%d", &a);
if (Second - First == 2) {
printf("%d and %d are twin primes.", First, Second);
} else {
printf("%d and %d are not twin primes.", First, Second);
}
return 0;
}



Aucun commentaire:

Enregistrer un commentaire