homework.wenqian.dev
← Back to index
Week 12026-05-21

C Basics

Linux Commands · C Declarations · Data Types · Control Flow

Lecture Notes

1. Common Linux Commands

CommandDescriptionExample
pwdPrint current working directorypwd
lsList directory contentsls -l
cdChange directorycd ~/Desktop
mkdirCreate a new directorymkdir lab1
rmRemove a filerm hello.c
cpCopy a filecp a.c b.c
mvMove or rename a filemv old.c new.c
catPrint file contentscat hello.c
gccCompile a C programgcc 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:

hello.c
#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 printf and scanf.
  • int main()Execution starts here. The int means 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

TypeMeaningTypical rangeprintf / scanf specifier
intInteger−2,147,483,648 to 2,147,483,647%d
doubleDouble-precision float~15 significant digits%lf
charSingle character (stored as integer)0 to 127 (ASCII)%c
types.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=A

4. Control Flow

if-else

branch.c
int x;
scanf("%d", &x);
if (x > 0) {
    printf("positive\n");
} else if (x < 0) {
    printf("negative\n");
} else {
    printf("zero\n");
}

while loop

while.c
int i = 1, sum = 0;
while (i <= 100) {
    sum += i;
    i++;
}
printf("1+2+...+100 = %d\n", sum);  // Output: 5050

for loop

for.c
int sum = 0;
for (int i = 1; i <= 100; i++) {
    sum += i;
}
printf("1+2+...+100 = %d\n", sum);  // Output: 5050

Assignments

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.
How to Compile and Run

C (using problem 1 as an example)

Step 1 — Compile the .c file into an executable using gcc:

terminal
gcc gcd.c -o gcd

Step 2 — Run the executable, then type your input and press Enter:

terminal
./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:

terminal
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

1Greatest Common Divisor (GCD)

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

terminal
gcc gcd.c -o gcd && ./gcd
terminal
python3 gcd.py
Hint: The straightforward approach: loop from 1 to min(a, b) and keep track of the largest number that divides both. Advanced approach (Euclidean algorithm): GCD(a, b) = GCD(b, a % b). Keep replacing until the remainder is 0 — the last non-zero value is the answer.
2Prime Number Check

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

terminal
gcc prime.c -o prime && ./prime
terminal
python3 prime.py
Hint: Straightforward approach: use a loop to check every number from 2 to n−1 and see if any of them divides n. Notice that sample 4 has n = 999,983 (a prime). If your loop runs all the way to n−1 it will be slow for large inputs. Key observation: if n has a factor, the smaller one must be ≤ √n. So you only need to check up to √n. In C, use i * i <= n as the loop condition to avoid computing a square root.
3Perfect Number Check

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

terminal
gcc perfect.c -o perfect && ./perfect
terminal
python3 perfect.py
Hint: Loop from 1 to n−1, add up every number that divides n evenly, then compare the total to n. Edge case: for n = 1, there are no proper divisors, so the sum is 0, which is not equal to 1 → output NO. Remember to include 1 in the sum, since 1 is a proper divisor of every integer greater than 1.
— Week 1 End —