Take a positive integer input from the user. Check whether the number is prime or not.

0 votes
asked Nov 29, 2017 by farrukh

3 Answers

+1 vote
answered Dec 3, 2017 by Fomo Peto (160 points)
#include <stdio.h>

int main()

{int num, i, remain=0;

printf("Enter a number to check if it is prime or not: \n");

scanf("%d", &num);

for(i=2;i<num;i++)
{remain =num%i;
    if(remain!=0)
    continue;
    else
    {printf("%d is not a prime number", num);
    break;}
}

if(i==num)

printf("%d is a prime number", num);

return 0;}
0 votes
answered Jan 8, 2018 by sruthi n (220 points)
#include<stdio.h>
void main()
{
  int n,i,c=0;
printf("enter a number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
 if(n%i==0)
 c++;
}
if(c==2)
printf("%d is a prime number",n);
else
printf("%d is not a prime number",n);
}
0 votes
answered Jan 23, 2018 by anonymous
#include<stdio.h>
#include<math.h>
void main()
{
    int n;
    printf("Enter a number:");
    scanf("%d",&n);
    int i,j=sqrt(n),f=0;
    for(i=2;i<=j;i++)
    {
        if(n%i==0)
        {
            f=1;
            break;
        }
    }
    if(f==1)
    printf("Not Prime");
    else
    printf("Prime");
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...