fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. #include <stdio.h>
  5. #include <math.h> // Required for the sqrt() function
  6.  
  7. // Function to check if a number is prime
  8. int isPrime(int num) {
  9. // Numbers less than 2 are not prime
  10. if (num <= 1) {
  11. return 0;
  12. }
  13. // Check for factors from 2 up to the square root of the number
  14. for (int i = 2; i <= sqrt(num); i++) {
  15. if (num % i == 0) {
  16. return 0; // If divisible, it's not prime
  17. }
  18. }
  19. return 1; // If no factors are found, it's prime
  20. }
  21.  
  22. int main() {
  23. printf("Prime numbers from 1 to 100 are:\\n");
  24.  
  25. // Loop through numbers from 1 to 100
  26. for (int i = 1; i <= 100; i++) {
  27. // If the number is prime, print it
  28. if (isPrime(i)) {
  29. printf("%d ", i);
  30. }
  31. }
  32.  
  33. printf("\\n");
  34. return 0;
  35. }
  36.  
  37.  
  38. // your code goes here
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
Standard output is empty