lundi 9 novembre 2015

program that tells if a number is prime

you enter a number and the program will find if the number is prime or not so when I enter the number 7 for the first time it will show you 'the number is prime' then I enter 8 and it will show you 'the number is not prime' after that I re_enter the number 7 and it will show you 'the number is not prime' I don't know where is the problem please help me an example photo from here and my code is :

#include <stdio.h>
#include <stdlib.h>

int main (void){
int n;
int t;
int isPrime=0;
char var;

while(var!='q'){
    printf("q=quit  p=prime  :");
    fflush(stdin);
    scanf("%c",&var);
    if(var=='p'){
        printf("plz put the number value :");
        scanf(" %d",&n);
        for(t=2;t<=n/2;t++){
            if (n%t==0){
                isPrime=1;
                break;
            }
        }
        if(isPrime==0){
            printf("%d is a prime number\n",n);
        }
        else{
            printf("%d is not a prime number\n",n);
        }
    }
    else if(var=='q'){
        printf("thank you bye\n");
        break;
    }
    else{
        printf("a wrong letter\n");
    }
}
return 0;
}

Aucun commentaire:

Enregistrer un commentaire