Sunday, March 13, 2011

C Program to check whether a given number is a prime number

Program
#include <stdio.h>

int main()
{
  int num,i;

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

  if (num == 1) printf("\nNumber 1 is neither primie nor composite.\n");
  void exit();

  for(i=1;i<=num/2;i++)
    {
      if (num%i == 0 && i != 1 && num != 2 && num != 3)
 {
   printf("\n%d is not a prime number.\n",num);
   break;
 }
      else
 {
   if (i == num/2) printf("\n%d is a prime number.\n",num);
   continue;
 }
    }
  return 0;
}
Compilation, Run and Output
[sreedhar@manchu2 cprograms]$ gcc prime.c -o prime
[sreedhar@manchu2 cprograms]$ ./prime

Enter the number: 24

24 is not a prime number.
[sreedhar@manchu2 cprograms]$ ./prime

Enter the number: 23

23 is a prime number.
[sreedhar@manchu2 cprograms]$ ./prime

Enter the number: 2

2 is a prime number.
[sreedhar@manchu2 cprograms]$ ./prime

Enter the number: 1

Number 1 is neither primie nor composite.
[sreedhar@manchu2 cprograms]$

No comments:

PBS Script Generator: Interdependent dropdown/select menus in Javascript

PBS SCRIPT GENERATOR
SH/BASH TCSH/CSH
Begin End Abort

About Me

LA, CA, United States
Here I write about the battles that have been going on in my mind. It's pretty much a scribble.

Sreedhar Manchu

Sreedhar Manchu
Higher Education: Not a simple life anymore