fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdbool.h>
  4.  
  5. // Function to check if a number is prime
  6. bool isPrime(int n) {
  7. // 0 and 1 are not prime numbers
  8. if (n <= 1) {
  9. return false;
  10. }
  11. // Check for divisibility from 2 to sqrt(n)
  12. for (int i = 2; i <= sqrt(n); i++) {
  13. if (n % i == 0) {
  14. return false; // n is divisible by i, hence not prime
  15. }
  16. }
  17. return true; // n is prime
  18. }
  19.  
  20. int main() {
  21. printf("Prime numbers between 1 and 100 are:\n");
  22. // Iterate through numbers from 1 to 100
  23. for (int i = 1; i <= 100; i++) {
  24. if (isPrime(i)) {
  25. printf("%d ", i); // Print the prime number
  26. }
  27. }
  28. printf("\n");
  29. return 0;
  30. }
Success #stdin #stdout 0.02s 25684KB
stdin
100
stdout
#include <stdio.h>
#include <math.h>
#include <stdbool.h>

// Function to check if a number is prime
bool isPrime(int n) {
    // 0 and 1 are not prime numbers
    if (n <= 1) {
        return false;
    }
    // Check for divisibility from 2 to sqrt(n)
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) {
            return false; // n is divisible by i, hence not prime
        }
    }
    return true; // n is prime
}

int main() {
    printf("Prime numbers between 1 and 100 are:\n");
    // Iterate through numbers from 1 to 100
    for (int i = 1; i <= 100; i++) {
        if (isPrime(i)) {
            printf("%d ", i); // Print the prime number
        }
    }
    printf("\n");
    return 0;
}