Search
 
SCRIPT & CODE EXAMPLE
 

C

prime check in c

int isPrime(long long number) {

	if (number == 0 || number == 1 || (number % 2 == 0 && number > 2)) {
		return 0;
	}

	else {
		for (long long i=3; i <= (long long)sqrt(number)+1; i++) {
			if (number % i == 0) {
				return 0;
			}
		}
		return 1;
	}
}
Comment

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:
Comment

check prime number or not c

#include <stdio.h>

int main() {

  int n, i, flag = 0;
  printf("Enter a positive integer: ");
  scanf("%d", &n);


  if (n == 0 || n == 1)
    flag = 1;

  for (i = 2; i <= n / 2; ++i) {


    if (n % i == 0) {
      flag = 1;
      break;
    }
  }

  // flag is 0 for prime numbers
  if (flag == 0)
    printf("%d is a prime number.", n);
  else
    printf("%d is not a prime number.", n);

  return 0;
}
Comment

check whether a number is prime or not in c

int n=7,i,flag=0;
	for(i=2;i<n/2;i++){
		if(n%i==0){
			flag=1;
			break;
		}
	}

	if(flag==0){
	printf("Prime number");

	}else{
		printf("not prime number");
	}
Comment

PREVIOUS NEXT
Code Example
C :: ROUNDING decimal number in C 
C :: get range of values in mongodb 
C :: how to checkout branch from commit id 
C :: multiplicationin c 
C :: c programing strtok use 
C :: how to pass an array of structs as an argument in c 
C :: c# for loop decrement 
C :: how to modulo in c without use the operator 
C :: c strcat 
C :: slug urls django 
C :: C Program to Find Largest and Smallest Number among N 
C :: how to make sure input is integer c 
C :: how to open a file with open in c 
C :: c program to find the frequency of all characters in a string 
C :: measure time in c 
C :: ft_putchar 
C :: typedef vs #define 
C :: memcpy in c 
C :: selection sort c 
C :: casting in c 
C :: c print characters 
C :: how to change file permissions in C language 
C :: c assignment operators 
C :: command args c 
C :: Find the how many bits is turned on in a numebr 
C :: get docker 
C :: c read file from command line 
C :: printing a string with putchar 
C :: Multiplying a u64 to u128 in Rust 
C :: divide a linked list into three parts based on their position mod 3. 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =