Q:

Bug in C program written to check prime number

I have written following program to check if given number is prime or not. But somehow program gives multiples of 5 like 15,25 as prime numbers. Where is bug? 
#include <stdio.h>

int main() {
    int number, i;

    printf("Enter the number to be checked\n");
    scanf("%d", &number);
    for (i = 2; i < number / 2; i++) {
        if (number % i == 0) {
            printf("The given number %d is not a prime number \n", number);
            break;
        } else {
            printf("The given number %d is a prime number \n", number);
            break;
        }
    }
    return 0; 
}

A:

So, a couple of things...
The main problem is that you're breaking out of your loop early whether the number is divisible by i or not:
if ( num % i == 0 )
  break;
else
  break;

You only want to break out of the loop early if the number is evenly divisible by i; otherwise, you check the against the next value of i:
for ( i = 2 ; i < number/2; i++ )
{
  if ( num % i == 0 )
    break;
}

After the loop, check to see if i is less than number / 2; if it is, then number is not prime:
if ( i < number / 2 )
  // number is not prime
else
  // number is prime

There are a couple of ways you can speed up your primality test.  First of all, you only need to test up to the square root of number, rather than number / 2.  Secondly, you can eliminate checks against even numbers; if a number is divisible by 2, then it's divisible by any even number, so you only need to check against 2 once.  In code:
int prime = 0; // initially false

if ( number == 2 ) // 2 is a special case, check separately
{
  prime = 1;
}
else if ( number < 2 || number % 2 == 0 )
{
  prime = 0; // negative numbers, 0, 1, and even numbers are not prime
}
else
{
  prime = 1; // assume prime to begin with
  for ( int i = 3; prime && i * i < number; i += 2 ) // continue while prime is true
  {                                                  // and i is less than sqrt(number)
    prime = number % i;
  }
}

printf( "%d is%s prime\n", number, prime ? "" : " not" ); 

