Learning C language requires hands-on practice and a lot of coding. I have compiled some classic programs that are essential for learning C language. I hope you can remember, understand, and apply them proficiently during your practice.
1. Output the 9*9 multiplication table.
#include <stdio.h>
int main() {
int i, j, result;
for(i = 1; i < 10; i++) {
for(j = 1; j < 10; j++) {
result = i * j;
printf("%d*%d=%-3d", i, j, result); /* -3d means left aligned, occupying 3 spaces */
}
printf("\n"); /* New line after each row */
}
}
2. Given a pair of rabbits, starting from the third month after birth, each month they produce another pair of rabbits. The young rabbits also start producing a pair after reaching the third month. Assuming no rabbits die, what is the total number of rabbits each month?
int main() {
long f1, f2;
int i;
f1 = f2 = 1;
for(i = 1; i <= 20; i++) {
printf("%12ld%12ld", f1, f2);
if(i % 2 == 0) printf("\n"); /* Control output, four per line */
f1 = f1 + f2; /* Sum of the previous two months assigned to the current month */
f2 = f1 + f2; /* Sum of the previous two months assigned to the current month */
}
}
3. A number is called a