-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathlab2_3.c
More file actions
61 lines (48 loc) · 1.21 KB
/
lab2_3.c
File metadata and controls
61 lines (48 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <math.h> // for sqrt function
#include <stdio.h>
/*
Task:
Write a function `int is_prime(int n)` that returns 1 if n is prime,
0 otherwise.
In main():
- Ask user for an integer n (>= 2)
- If invalid, print an error
- Otherwise, print all prime numbers up to n
*/
int is_prime(int n) {
// TODO: check if n is prime using loop up to sqrt(n)
if (n < 2) return 0; // numbers less than 2 are not prime
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return 0; // found a divisor → not prime
}
}
return 1; // no divisors found → prime
}
int main(void) {
int n;
printf("Enter an integer n (>= 2): ");
scanf("%d", &n);
// TODO: validate input and print all primes up to n
if (n < 2) {
printf("Error: n must be >= 2\n");
return 1;
}
printf("Prime numbers up to %d:\n", n);
for (int i = 2; i <= n; i++) {
if (is_prime(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
// TODO: check if n is prime using loop up to sqrt(n)
return 0; // placeholder
}
int main(void) {
int n;
printf("Enter an integer n (>= 2): ");
scanf("%d", &n);
// TODO: validate input and print all primes up to n.
return 0;
}