Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my program to check for prime numbers showing numbers with last digit 5 as prime numbers?

I wrote a simple and short program to check for prime numbers in C. It is showing normal results for all numbers except for those numbers which have last digit as 5 by printing them as prime numbers, even those numbers which are clearly not prime, like 15, 25, 45 etc.

My question is that why is it doing so and how can I fix it?

Here is my code -:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include<stdio.h>

int main()
{
    int num, count=0;

    printf("\nEnter a number : ");
    scanf("%d",&num);

    for(int i=2; i<=num/2; i++)
       {
         if(num%i==0)
            count=1;   
            break; 
       }

    if(count==1)
      printf("\nNot a prime number.");
    else
      printf("\nPrime Number.");  
}

Any help would be great.

Thank You.

>Solution :

#include<stdio.h>

int main()
{
    int num, count=0;

    printf("\nEnter a number : ");
    scanf("%d",&num);

    for(int i=2; i<=num/2; i++)
       {
         if(num%i==0){ // you have to add {} for multi-line code 
            count=1;   
            break; 
         }
       }

    if(count==1)
      printf("\nNot a prime number.");
    else
      printf("\nPrime Number.");  
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading