“periksa bilangan prima atau tidak c” Kode Jawaban

Check in C Prime 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;
	}
}
Nasty Narwhal

Program pemeriksaan bilangan prima di 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:
Repulsive Rattlesnake

periksa bilangan prima atau tidak 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;
}
SaadMakhdoom

Periksa apakah nomornya adalah prima di 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!");
    }
}
Splendid Squirrel

Jawaban yang mirip dengan “periksa bilangan prima atau tidak c”

Pertanyaan yang mirip dengan “periksa bilangan prima atau tidak c”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya