Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions C/prime_checker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Prime Number Checker

A C program to check if a number is prime or not.

## What it does

Takes a number as input and tells you whether it's prime or not. Pretty straightforward.

## How to compile

```bash
gcc prime_checker.c -o prime_checker -lm
```

The `-lm` flag is needed for the math library.

## Usage

```bash
./prime_checker
```

Then just enter a number when it asks.

## Examples

```
Prime Number Checker
Enter a number: 17
17 is Prime
```

```
Prime Number Checker
Enter a number: 24
24 is Not Prime
```

## How it works

The program checks divisibility up to the square root of the number. If nothing divides it evenly, it's prime. Numbers less than 2 aren't considered prime.

## Test it with these

Some primes to try: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97

Some non-primes: 4, 6, 8, 9, 10, 12, 15, 16, 18, 20, 21, 24, 25, 27, 28, 30

37 changes: 37 additions & 0 deletions C/prime_checker/prime_checker.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdbool.h>
#include <math.h>

bool isPrime(int num) {
if (num <= 1) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;

int limit = (int)sqrt(num);
for (int i = 3; i <= limit; i += 2) {
if (num % i == 0) return false;
}

return true;
}

int main() {
int number;

printf("Prime Number Checker\n");
printf("Enter a number: ");

if (scanf("%d", &number) != 1) {
printf("Invalid input!\n");
return 1;
}

if (isPrime(number)) {
printf("%d is Prime\n", number);
} else {
printf("%d is Not Prime\n", number);
}

return 0;
}

Loading