Prime Number – Different Models


/* Prime Number Program – Example for goto label */

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

int n, i, r;

clrscr();

printf(“Enter the positive integer value…”);

scanf(“%d”, &n);

i=2;

step1:

if(i<=sqrt(n))

{

r=n%i;

if(r==0)

{           printf(“%d is not a prime”, n);

goto end;

}

}

else

{           i++;

goto step1;

}

printf(“%d is prime”, n);

getch();

end:

printf(“ “);

}

/* Prime Number – Using Functions */

#include<stdio.h>

void main()

{           void prime(int);

int n;

printf(“Enter a Number: ”);

scanf(“%d”, &n);

prime(n);

}

void prime(int k)

{           int i, flag=0;

if(k <= 3)

flag=1;

else

{

for(i=2; i<=k/2; i++)

{

if(k%i==0)

{

printf(“Number %d is not Prime\n”, k);

flag=0;

break;

}

else

flag=1;

}

}

if(flag==1)

printf(“Number %d is Prime\n”, k);

}

Advertisements