Radical Developers welcomes all the visitors to be a member of the team. Come join us. Its all about open-source concept. All Programs are written and tested in MacOS X, Unix, Linux and may not match with the outputs of Turbo C++ in Windows

Tuesday, January 4, 2011

Write a C Program to check whether a number is Prime or not

Source Code :

#include<stdio.h>
void main()
{
      int n,i,fl=0;     //Declaration of variables
      clrscr();
      printf("\nEnter a Number : ");
      scanf("%d",&n);   //Initialization of number from user
      for(i=2;i<n;i++)    //For Loop for checking prime number
      {
            if(n%i==0)  //Checking if the number can be divided
            {                        //by any other number other
                  fl=1;
            }
      }

      if(fl==1)   //Checking for result
            printf("\n%d is Not a Prime Number ",n); //Display Result
      else
            printf("\n%d is a Prime Number ",n);
      printf(“\nPress any Key to Continue………”);
      getch();
}

Output :

Enter a Number : 11
11 is a Prime Number 
Press any Key to Continue……

Enter a Number : 4
4 is Not a Prime Number
Press any Key to Continue…………

No comments:

Post a Comment