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.

alhorithm of prime number

+1 vote
asked May 9, 2020 by MRINMOY DEBNATH (130 points)

3 Answers

0 votes
answered May 12, 2020 by LiOS (6,460 points)

In C:

#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) {

        // condition for non-prime
        if (n % i == 0) {
            flag = 1;
            break;
        }
    }

    if (n == 1) {
        printf("1 is neither prime nor composite.");
    }
    else {
        if (flag == 0)
            printf("%d is a prime number.", n);
        else
            printf("%d is not a prime number.", n);
    }

    return 0;
}
0 votes
answered May 12, 2020 by Flash (140 points)
#include<stdio.h>
int main()
{
    int n,i=2;
    printf("Enter a number:");
    scanf("%d",&n);
    while(n%i!=0)
    {
        i++;
    }
    if(i==n)    printf("%d is a prime number",n);
    else        printf("%d is a not prime number",n);
    return 0;
}
0 votes
answered May 13, 2020 by Yuvraj Jwala (140 points)
/* c programming*/
/* compute weather no. is prime or not */
/*YUVRAJ JWALA */
#include <stdio.h>
int main()
{
   int a,i,count=0;
   printf("write a number\n");
   scanf("%d",&a);
   for (i=1;i<=a;i++)
   {
       if (a%i==0)        /*THIS IS THE ALOGRITHM OF PRIME NUMBER*/
          count++;
   }
   (count>2)?printf("%d is not prime number",a):printf("%d is prime number",a);
   return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...