Monday 17 April 2017

A program for prime number to check whether the given number is prime or not in C

Dear Students ,

   Do you know ? 
     
Q .   What is prime Numbers ?

Ans . Prime numbers or those number which are divided by 1 or it'self . It's mean only two times .

For Example : 

    num= 7;

  if you try to divide the above number , you will get only two time reminder 0 when you divide by 1 and divide by 7 between 1 to num numbers .  


Let's Example with program : 

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
       int num , i,d=0;
         printf("Enter your Number : ");
        scanf("%d",&num);
       
        for(i=1;i<=num;i++) 
         {
             if(num%i==0)
              {
                  d++;
               }
          }
          
         if(d==2)
         {
           printf("Number is Prime ");
         }
        else
          {
            printf("Number is not Prime");
         }
  getch();
    }

  Your OutPut : 


2 comments:

  1. Nice. Only problem is the efficiency of the code. Lets say, you have to check if 2222334558765544346. The for loop will unnecessarily keep running​ even when you know in the 2nd iteration itself, that it's not a prime number.

    From now on, consider the efficiency of code and logic.

    ReplyDelete