Answers for "code c prime number"

C
3

prime numbers c

#include<stdio.h>
void main()
{
    int num;
    int prime = 1;
    printf("Enter a number = ");
    scanf("%d",&num);


    for(int i = 2; i < num; i++)
    {
        if(num % i == 0)
        {
            prime = 0;
            break;
        }
    }
    if(prime == 1 && num>0 && num !=1)
    {
        printf("%d is a prime number.", num);
    }
    else
    {
        printf("%d isn't a prime number.", num);
    }
}
Posted by: Guest on March-10-2022
2

Prime Number Check Program in C

#include <stdio.h> 

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;    
}
Program Output:
Posted by: Guest on January-01-2022
2

prime number c program

int isPrime(int n) {
  for (int i = 2; i < n; i++) if (n % i == 0) return 0; 
  return 1;
}
Posted by: Guest on March-25-2021

Code answers related to "C"

Browse Popular Code Answers by Language