c program to check number is prime or not


                        This program is for finding that number is prime or not. Prime no. are those numbers which ,divide by 1 and itself only. For example 7,13, etc.

C program to check number is prime or not

#include<stdio.h>

void main()
{
       int no,i,flag=0;
       clrscr();

       printf("Enter a no::");
       scanf("%d",&no);

       for(i=2;i<no;i++)
       {
              if(no%i==0)
              {
                    flag=1;
              }

       }
       if(flag==1)
       {
              printf("Number is not prime no.");
       }
       else
       {
             printf("Number is a prime no.");
       }

       getch();

}

c program to check number is prime or not


2 comments:

  1. it should be for(I=1) not for(I=2).......

    ReplyDelete
    Replies
    1. no friend its i=2 because prime number is divisible by itself. If we set i=1 than all number%i is true(11%1==0) and flag will be 1 so it shows that 11 is not prime thats why we need to start loop from 2 instead of 1.

      Delete