Answers for "in c prime number or even number"

C
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
0

check if the number is prime in c

#include <stdio.h>

void main() {
    int number;

    printf("\n\n\tEnter the number and we will check, if the number is prime or not : ");
    scanf("%d", &number);

    if (number == 0 || number == 1 || (number % 2 == 0 && number > 2))
    {
        printf("\n\tThe entered number is not a prime number!");
    }
    else
    {
        printf("\n\tThe entered number is a prime number!");
    }
}
Posted by: Guest on March-18-2022

Code answers related to "C"

Browse Popular Code Answers by Language