Program to check for prime number:
Solution:
#include <stdio.h> int main() { int n, i, c = 0; printf("Enter any number n:"); scanf("%d", &n); /*logic*/ for (i = 1; i <= n; i++) { if (n % i == 0) { c++; } } if (c == 2) { printf("n is a Prime number"); } else { printf("n is not a Prime number"); } return 0; }
Prime number between two number is given by:
Solution:
#include <stdio.h> int main() { int n, i, c = 0; printf("Enter any number n:"); scanf("%d", &n); /*logic*/ for (i = 1; i <= n; i++) { if (n % i == 0) { c++; } } if (c == 2) { printf("n is a Prime number"); } else { printf("n is not a Prime number"); } return 0; }
Prime number between two number is given by:
#include <stdio.h>
int main()
{
int n1, n2, i, j, flag;
printf("Enter two numbers(intevals):");
scanf("%d %d", &n1, &n2);
printf("Prime numbers between %d and %d are: ", n1, n2);
for(i=n1+1; i<n2; ++i)
{
flag=0;
for(j=2; j<=i/2; ++j)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%d ",i);
}
return 0;
}
No comments:
Post a Comment