Recommended Reading
25,000 words, 80 images summarizing computer network knowledge, hurry up and bookmark it
Programmer health schedule, 99% of programmers can’t achieve it
Recursive calls in C language clarified by these 13 questions
20 commonly used macro definitions in C language to prevent errors
Detailed explanation of error handling and exception handling in C language, understand it thoroughly in one article
1. Output the 9*9 multiplication table. There are 9 rows and 9 columns, with i controlling the rows and j controlling the columns.
//
// Created by 冲哥.
//
#include "stdio.h"
int main() {
int i, j, result;
for (i = 1; i < 10; i++) {
for (j = 1; j <= i; j++) {
result = i * j;
printf("%d*%d=%-4d", i, j, result);
}
printf("\n");
}
return 0;
}
2. Classical Problem: A pair of rabbits, starting from the third month after birth, each month they produce a pair of rabbits. The young rabbits, after reaching the third month, also produce a pair of rabbits each month. If the rabbits do not die, what is the total number of rabbits each month?
//
// Created by 冲哥.
//
#include "stdio.h"
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; /* Assign the sum of the previous two months to the third month */
f2 = f1 + f2; /* Assign the sum of the previous two months to the third month */
}
return 0;
}
3. Determine how many prime numbers are there between 101 and 200, and output all prime numbers and their count.
Program analysis: The method to determine a prime number: divide a number by all integers from 2 to sqrt(this number). If it can be divided evenly, then this number is not a prime number; otherwise, it is a prime number.
//
// Created by 冲哥.
//
#include "stdio.h"
#include "math.h"
int main() {
int m, i, k, h = 0, leap = 1;
printf("\n");
for (m = 101; m <= 200; m++) {
k = sqrt(m + 1);
for (i = 2; i <= k; i++)
if (m % i == 0) {
leap = 0;
break;
}
if (leap) /* If leap remains 1 after the inner loop ends, then m is a prime number */
{
printf("%-4d", m);
h++;
if (h % 10 == 0)
printf("\n");
}
leap = 1;
}
printf("\nThe total is %d", h);
return 0;
}
4. A number is called a “perfect number” if it is equal to the sum of its factors. For example, 6 = 1 + 2 + 3. Write a program to find all perfect numbers within 1000.
//
// Created by 冲哥.
//
#include "stdio.h"
int main() {
static int k[10];
int i, j, n, s;
for (j = 2; j < 1000; j++) {
n = -1;
s = j;
for (i = 1; i < j; i++) {
if ((j % i) == 0) {
n++;
s = s - i;
k[n] = i;
}
}
if (s == 0) {
printf("%d is a perfect number: ", j);
for (i = 0; i < n; i++)
printf("%d,", k[i]);
printf("%d\n", k[n]);
}
}
return 0;
}
5. Program to print a right-angled Pascal’s triangle
//
// Created by 冲哥.
//
#include "stdio.h"
int main() {
int i, j, a[6][6];
for (i = 0; i <= 5; i++) {
a[i][i] = 1;
a[i][0] = 1;
}
for (i = 2; i <= 5; i++)
for (j = 1; j <= i - 1; j++)
a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
for (i = 0; i <= 5; i++) {
for (j = 0; j <= i; j++)
printf("%4d", a[i][j]);
printf("\n");
}
return 0;
}
6. Reverse the input string and output it
//
// Created by 冲哥.
//
#include "stdio.h"
#include <string.h>
int main() {
char c[200], c1;
int i, j, k;
printf("Enter a string: ");
scanf("%s", c);
k = strlen(c);
for (i = 0, j = k - 1; i < k / 2; i++, j--) {
c1 = c[i];
c[i] = c[j];
c[j] = c1;
}
printf("%s\n", c);
return 0;
}
7. Find a substring s2 in a string s1, if it exists, return the starting position of the substring in the main string, otherwise return -1.
//
// Created by 冲哥.
//
#include "stdio.h"
#include <string.h>
int main() {
char s1[6] = "thisis";
char s2[5] = "is";
printf("%d\n", search(s1, s2));
return 0;
}
int search(char s1[], char s2[]) {
int i = 0, j, len = strlen(s2);
while (s1[i]) {
for (j = 0; j < len; j++)
if (s1[i + j] != s2[j]) break;
if (j >= len)return i;
else i++;
}
return -1;
}
8. Input a string and determine whether it is a palindrome. A palindrome is a string that reads the same forwards and backwards.
//
// Created by 冲哥.
//
#include <stdio.h>
#include <string.h>
#include<string.h>
int main() {
char s[100];
int i, j, n;
printf("Enter a string:\n");
gets(s);
n = strlen(s);
for (i = 0, j = n - 1; i < j; i++, j--)
if (s[i] != s[j]) break;
if (i >= j) printf("It is a palindrome\n");
else printf("It is not a palindrome\n");
return 0;
}
9. Use pointer variables to output elements of a structure array.
//
// Created by 冲哥.
//
#include <stdio.h>
struct student {
int num; // Student ID
char *name; // Name
char sex; // Gender
int age; // Age
} stu[5] = {{1001, "张三", 'F', 18},
{1002, "李四", 'M', 28},
{1003, "王五", 'F', 34},
{1004, "赵六", 'F', 25},
{1005, "前七", 'M', 19}};
int main() {
int i;
struct student *ps;
printf("Num \tName\t\t\tSex\tAge\t\n");
// Use pointer variables to output elements of the structure array.
for (ps = stu; ps < stu + 5; ps++)
printf("%d\t%-10s\t\t%c\t%d\t\n", ps->num, ps->name, ps->sex, ps->age);
printf("************************************\n");
// Use array subscript method to output student ID and age of the structure array elements.
for (i = 0; i < 5; i++)
printf("%d\t%d\t\n", stu[i].num, stu[i].age);
return 0;
}
![8 Basic and Practical Classic Examples in C Language [Source Code Included]](https://boardor.com/wp-content/uploads/2025/11/1d7c3fda-40c8-430a-b0df-6729fee07c32.png)
![8 Basic and Practical Classic Examples in C Language [Source Code Included]](https://boardor.com/wp-content/uploads/2025/11/a9df2ff3-6b17-4366-8bb3-affeb6b06d3e.png)