Testing.

Tuesday, 3 January 2017

C programming language: Prime number program in c.


// simple code but not optimized much.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,flag=0;
printf("\nEnter Number to Check Prime: ");
scanf("%d",&n);
for(i=2;i<n/2;i++)
{
if(n%i==0)
{
flag=1;
        break;
}

else
flag=0;
}
if(flag==0)
printf("\nGiven Number is prime:");
else
    printf("\nGiven Number is Not prime: ");
getch();
}




// simple and optimized code. 
#include<stdio.h>
#include<conio.h>
void main()
{
int p=2;
int n,num;
printf("\nEnter Number to Check Prime: ");
scanf("%d",&num);
n=num;
while(num%p++!=0);  // dummy while loop 
if(p>num)
printf("\nGiven Number is prime:");
else
printf("\nGiven Number is Not prime:");
getch();
}




No comments:

Post a Comment