lundi 26 octobre 2015

C program to check whether a number is prime or not

I used the following code and it showed numbers like 49 as prime and not composite. I am new to programming so please help me with the correct code.

#include <stdio.h>
int main()
{
    int n;
    int i;
    scanf ("%d", &n);
    for (i=2; i<n; i++)
    {
        if (n%i==0)
        {
            printf ("number is composite");
        }
        else
        {
            i=i+1;
        }  
    }
    printf("number is prime");
    return 0;

}

Aucun commentaire:

Enregistrer un commentaire