Wednesday 13 April 2016

Solution of URI 1165 :: Prime Number


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
#include <math.h>

int prime(int x)
{
    int i, root;
    if(x<2)
        return 0;
    if(x==2)
        return 1;
    if(x%2==0)
        return 0;
    root=sqrt(x);
    for(i=3; i<=root; i=i+2)
    {
        if(x%i==0)
            return 0;
    }
    return 1;
}
int main()
{
    int X,a,N;
    scanf("%d", &N);
    for(a=1;a<=N;a++)
    {
        scanf("%d", &X);
        if(1==prime(X))
            printf("%d eh primo\n", X);
        else if(0==prime(X))
            printf("%d nao eh primo\n", X);
    }
    return 0;
}

3 comments:

  1. #include
    int main()
    {
    int i,n,j,x,y=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    scanf("%d",&x);
    for(j=2;j<x;j++)
    {
    if(x%j==0)
    y++;
    else
    continue;
    }
    if(y!=0)
    printf("%d nao eh primo\n",x);
    else
    printf("%d eh primo\n",x);
    x=0;
    y=0;

    }
    return 0;
    }
    reply...

    ReplyDelete
  2. #include
    int main()
    {
    int T,n,i,j,d=0;
    scanf("%d",&T);
    for(i=1;i<=T;i++)
    {
    scanf("%d",&n);
    d=0;

    for(j=1;j<=n;j++)
    {
    if(n%j==0)
    {
    d++;
    }
    }
    if(d==2)
    {
    printf("%d eh primo\n",n);
    }

    else
    {
    printf("%d nao eh primo\n",n);
    }

    }

    }

    ReplyDelete

Note: only a member of this blog may post a comment.