C Online Compiler
Example: Naive Prime Number Finder in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Naive Prime Number Finder #include <stdio.h> #include <stdbool.h> // For boolean type int main() { int lower_bound, upper_bound; // Step 1: Get user input for the range printf("Enter the lower bound of the range: "); scanf("%d", &lower_bound); printf("Enter the upper bound of the range: "); scanf("%d", &upper_bound); printf("Prime numbers between %d and %d are: ", lower_bound, upper_bound); // Step 2: Iterate through each number in the range for (int num = lower_bound; num <= upper_bound; num++) { // Step 3: Handle special cases: numbers less than 2 are not prime if (num <= 1) { continue; // Skip numbers that are 1 or less } // Step 4: Check for divisibility from 2 up to num-1 bool is_prime = true; for (int i = 2; i < num; i++) { if (num % i == 0) { is_prime = false; // Found a divisor, so not prime break; // No need to check further } } // Step 5: If no divisors were found, print the number if (is_prime) { printf("%d ", num); } } printf("\n"); return 0; }
Output
Clear
ADVERTISEMENTS