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 gcdStep 2 — Run the executable, then type your input and press Enter:
./gcd12 18 ← your input6 ← program outputPython (using problem 1 as an example)
Python does not need compilation — run the .py file directly:
python3 gcd.py12 18 ← your input6 ← program outputSuggested 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 ≤ 10000Output format
One line containing the GCD of a and b.Sample 1
Input
12 18Output
6Sample 2
Input
100 75Output
25Sample 3
Input
7 13Output
1Files: gcd.c and gcd.py
gcc gcd.c -o gcd && ./gcdpython3 gcd.pyA 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,000Output format
Print YES if n is prime, or NO if it is not.Sample 1
Input
7Output
YESSample 2
Input
12Output
NOSample 3
Input
2Output
YESSample 4
Input
999983Output
YESFiles: prime.c and prime.py
gcc prime.c -o prime && ./primepython3 prime.pyi * 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,000Output format
Print YES if n is a perfect number, or NO if it is not.Sample 1
Input
6Output
YESSample 2
Input
28Output
YESSample 3
Input
12Output
NOSample 4
Input
1Output
NOFiles: perfect.c and perfect.py
gcc perfect.c -o perfect && ./perfectpython3 perfect.py