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 :: c long to string 
C :: malloc 
C :: convert string to int c 
C :: how to input n space separated integers in c 
C :: int data types in c 
C :: c break statement 
C :: rfid rc522 code 
C :: c language float user input 
C :: char ASCII in c 
C :: Initialization of a 3d array in c 
C :: powershell list big files 
C :: c to fahrenheit 
C :: getline() in c 
C :: notation of positive in c 
C :: while loop in c 
C :: scopes in c 
C :: declaration of string in c 
C :: getchar declaration in c 
C :: marquee html code with right 
C :: type cast in c 
C :: vscode how to output in seperate consile 
C :: amazon kinesis disaster recovery 
C :: gdebi install with yes option 
C :: error: invalid type for iteration variable ‘i’ #pragma omp parallel for 
C :: How to scale all columns in dataframe in R? 
C :: 157.245.33.77: 
C :: if statement shortcut c 
C :: WAP to create Database using array of structure & display it in C 
C :: return multiple values using the call by reference 
C :: how to make play a song javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =