Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

C PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS PRIME OR NOT

0 votes
asked Mar 5, 2019 by EPHRAIM

2 Answers

+1 vote
answered Mar 5, 2019 by Ankith Rathore (160 points)
edited Mar 5, 2019 by Ankith Rathore
#include <stdio.h>
int main()
{
    int n, i, flag = 0;

    printf("Enter a positive integer: ");
    scanf("%d",&n);

    for(i=2; i<=n/2; ++i)                      /*here actually we are supposed to use (i<=n-1)  instead of  (i<=n/2),but both will give the answer*/
    {
        // condition for nonprime number
        if(n%i==0)
        {
            flag=1;
            break;
        }
    }

    if (flag==0)
        printf("%d is a prime number.",n);
    else
        printf("%d is not a prime number.",n);
    
    return 0;
}
commented Mar 6, 2019 by Nxhid67 (100 points)
only one problem:
after a certain number the program outputs a random number, like when i typed 4637375545 it said "342408249 is not a prime number"
0 votes
answered Mar 6, 2019 by ashutosh0699 (150 points)
#include <stdio.h>

int main()
{
    int num,i,count=0;
    printf ("Enter the number which you want to check whether it is prime number or not \n");
    scanf ("%d",&num);
    for (i=2;i<=num/2;i++)
    {
        if (num%i==0)
        {
            count++;
            break;
        }
    }
    if (count==0 && num!=1)
    {
        printf("%d is prime \n",num);
    }
    else
    printf("%d is not prime \n",num);
        
    return 0;
}
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.
...