ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

Check weather a given no is Prime or Not using c programming

Updated on March 25, 2010

 #include<stdio.h>

#include<conio.h>

void main()

{

int n,i=2;

clrscr();

printf("Enter a number \n");

scanf("%d",&n);

while(i<=n/2)

{

if(n % i == 0)

{

printf("Not a Prime Number");

getch();

exit(0);

}

i=i+1;

}

printf("Prime Number");

getch();

exit(0);

}

----------------------------------------------------

NB: n % i == 0 -------> here the we get the remainder when n is divided by each i, at any time the remainder is 0, the program will terminate and show a message " not prime". otherwise it will execute till i reaches to n/2. and terminate the program and shows a message "prime".

thanking you.

hope u understand the logic. ur comments are valuable.

-----------------------------------------------------------

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      gurdiv singh 3 years ago

      Thnkxxx alot

    • profile image

      irfan 3 years ago

      good for understanding because of in simple language

    • profile image

      jitendra chavan 5 years ago

      i can understand

    • profile image

      RENU 6 years ago

      I am struggle with prime or not programme very very thanks to given this programme

    • profile image
      Author

      gaig 7 years ago

      OUTPUT: If we enter 9, the result will be "Not Prime"

      If we enter 11, the result will be "Prime"

    • ShallyD profile image

      ShallyD 7 years ago from USA

      thanks for this program.

      do write the output of this program

    • profile image
      Author

      gaig 7 years ago

      thanks a lot

    • pooja0908 profile image

      Pooja Sharma 7 years ago from Delhi

      very initial hub for c programming

      i think it will help newbie of c programming