Lecture Notes
1. Common Linux Commands
| Command | Description | Example |
|---|---|---|
| pwd | Print current working directory | pwd |
| ls | List directory contents | ls -l |
| cd | Change directory | cd ~/Desktop |
| mkdir | Create a new directory | mkdir lab1 |
| rm | Remove a file | rm hello.c |
| cp | Copy a file | cp a.c b.c |
| mv | Move or rename a file | mv old.c new.c |
| cat | Print file contents | cat hello.c |
| gcc | Compile a C program | gcc hello.c -o hello |
| ./ | Run a program in the current directory | ./hello |
2. Basic C Declarations
A minimal C program consists of the following parts:
#include <stdio.h> // Include the standard I/O library (header file)
int main() { // Program entry point — there must be exactly one main()
printf("Hello, World!\n"); // Print a line; \n is a newline character
return 0; // Return 0 to indicate the program finished successfully
}- #include <stdio.h>Tells the compiler to use the stdio (standard input/output) library, which provides
printfandscanf. - int main()Execution starts here. The
intmeans it returns an integer. - printf(...)Formatted output to the terminal. Format specifiers:
%d(integer),%lf(double),%c(char). - scanf(...)Read input from the terminal. Prepend
&to variables, e.g.scanf("%d", &n).
3. Three Basic Data Types
| Type | Meaning | Typical range | printf / scanf specifier |
|---|---|---|---|
| int | Integer | −2,147,483,648 to 2,147,483,647 | %d |
| double | Double-precision float | ~15 significant digits | %lf |
| char | Single character (stored as integer) | 0 to 127 (ASCII) | %c |
int age = 20;
double pi = 3.14159;
char letter = 'A';
printf("age=%d, pi=%.2lf, letter=%c\n", age, pi, letter);
// Output: age=20, pi=3.14, letter=A4. Control Flow
if-else
int x;
scanf("%d", &x);
if (x > 0) {
printf("positive\n");
} else if (x < 0) {
printf("negative\n");
} else {
printf("zero\n");
}while loop
int i = 1, sum = 0;
while (i <= 100) {
sum += i;
i++;
}
printf("1+2+...+100 = %d\n", sum); // Output: 5050for loop
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
printf("1+2+...+100 = %d\n", sum); // Output: 5050Assignments
Requirements
- 3 problems in total. Estimated time: 1 – 2 hours.
- Each problem requires two implementations: a C version (
.c) and a Python version (.py). - Read input from stdin (keyboard) and write output to stdout (terminal). Output format must match the samples exactly.
C (using problem 1 as an example)
Step 1 — Compile the .c file into an executable using gcc:
gcc gcd.c -o gcd
Step 2 — Run the executable, then type your input and press Enter:
./gcd 12 18 ← your input 6 ← program output
Python (using problem 1 as an example)
Python does not need compilation — run the .py file directly:
python3 gcd.py 12 18 ← your input 6 ← program output
Suggested file names — C: gcd.c, prime.c, perfect.c | Python: gcd.py, prime.py, perfect.py
The Greatest Common Divisor (GCD) of two integers is the largest positive integer that divides both of them without a remainder. For example, the common divisors of 12 and 18 are 1, 2, 3, and 6. The largest is 6, so GCD(12, 18) = 6.
Given two positive integers a and b, write a program that outputs their GCD.
Input format
One line containing two positive integers a and b separated by a space. Constraint: 1 ≤ a, b ≤ 10000
Output format
One line containing the GCD of a and b.
Sample 1
Input
12 18
Output
6
Sample 2
Input
100 75
Output
25
Sample 3
Input
7 13
Output
1
Files: gcd.c and gcd.py
gcc gcd.c -o gcd && ./gcd
python3 gcd.py
A prime number is a positive integer greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are prime. 4 (= 2×2), 6 (= 2×3), and 9 (= 3×3) are composite — they have divisors other than 1 and themselves.
Note: 1 is neither prime nor composite. The constraint guarantees n ≥ 2, so you do not need to handle n = 1.
Given a positive integer n, determine whether it is prime.
Input format
One line containing a single positive integer n. Constraint: 2 ≤ n ≤ 1,000,000
Output format
Print YES if n is prime, or NO if it is not.
Sample 1
Input
7
Output
YES
Sample 2
Input
12
Output
NO
Sample 3
Input
2
Output
YES
Sample 4
Input
999983
Output
YES
Files: prime.c and prime.py
gcc prime.c -o prime && ./prime
python3 prime.py
i * i <= n as the loop condition to avoid computing a square root.A proper divisor of a positive integer n is any positive integer that divides n evenly, excluding n itself. For example, the proper divisors of 12 are 1, 2, 3, 4, and 6.
A positive integer n is called a perfect number if the sum of all its proper divisors equals n itself.
- Proper divisors of 6: 1 + 2 + 3 = 6 ✓ perfect
- Proper divisors of 28: 1 + 2 + 4 + 7 + 14 = 28 ✓ perfect
- Proper divisors of 12: 1 + 2 + 3 + 4 + 6 = 16 ≠ 12 ✗ not perfect
Given a positive integer n, determine whether it is a perfect number.
Input format
One line containing a single positive integer n. Constraint: 1 ≤ n ≤ 100,000
Output format
Print YES if n is a perfect number, or NO if it is not.
Sample 1
Input
6
Output
YES
Sample 2
Input
28
Output
YES
Sample 3
Input
12
Output
NO
Sample 4
Input
1
Output
NO
Files: perfect.c and perfect.py
gcc perfect.c -o perfect && ./perfect
python3 perfect.py