This article summarizes many common problem types in C language, which can help everyone learn and review the basics of C language. If you have better solutions, feel free to leave a message!!!Reverse OrderOutput a given number in reverse order
// Reverse output of an input integer
#include <stdio.h>
int main() {
int i, a = 0, reverse = 0;
scanf_s("%d", &i);
while (i > 0) {
a = i % 10; // Get the last digit
reverse = reverse * 10 + a;
i = i / 10; // Shift left each loop
}
printf("%d", reverse);
return 0;
}
Reverse output of an input string
#include <stdio.h>
#include <string.h>
int main() {
int i, j, k;
char s[50], temp;
gets(s); // Input
k = strlen(s); // Calculate the length of the string
// Reverse output
for (i = 0, j = k - 1; i < k / 2; i++, j--) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
puts(s); // Output
}
Print all prime numbers between 100 and 200
#include <stdio.h>
#include <math.h>
int main() {
int i = 0;
int j = 0, k;
for (int i = 100; i <= 200; i++) {
// Check if it is a prime number
// 1. A number that can be divided by 1 and itself is a prime number
// Trial division method
int isPrime = 1; // Flag to determine if it is a prime number
k = sqrt(i);
for (int j = 2; j <= k; j++) {
if (i % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime) {
printf("%d\n", i);
}
}
return 0;
}
Calculate the amount payable based on membership level using switch statement
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main() {
int rank = 0; // Declare and initialize user membership level
float pay = 0, discount; // Declare payment amount and discount
printf("Please enter the user's membership level:\n");
scanf("%d", &rank);
printf("Please enter the total payment amount:\n");
scanf("%f", &pay);
switch (rank) {
case 1:
discount = 0.9f;
break;
case 2:
discount = 0.85f;
break;
case 3:
discount = 0.8f;
break;
default:
discount = 1.0f;
break;
}
pay = pay * 1;
printf("Amount payable before discount: %f.\n", pay);
pay = pay * discount;
printf("Amount payable after discount: %f.\n", pay);
return 0;
}
Calculate the length of a string
#include <stdio.h>
int main() {
int len = 0;
char c;
while ((c = getchar()) != '\n') {
len++;
}
printf("The length of the string is: %d\n", len);
return 0;
}
Input the quantity and unit price of goods, calculate the total amount
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main() {
int amount;
float price, total;
printf("Please enter the quantity of goods:");
scanf("%d", &amount);
printf("Please enter the price of goods:");
scanf("%f", &price);
total = amount * price;
printf("Total amount of goods is %f yuan.\n", total);
return 0;
}
Input the lengths of the three sides of a triangle, a, b, c, and calculate the area of the triangle
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, s, h;
printf("Please enter the lengths of the three sides of the triangle:\n");
scanf("%f%f%f", &a, &b, &c);
if (a <= 0 || b <= 0 || c <= 0) {
printf("Input error!\n");
return 0;
}
if (a + b > c && a + c > b && b + c > a) {
h = (a + b + c) / 2.0; // Perimeter
s = sqrt(h * (h - a) * (h - b) * (h - c)); // Heron's formula
printf("Area of the triangle: %f\n", s);
} else {
printf("Invalid input!\n");
}
return 0;
}
Print Pascal’s triangle with 10 rows and 10 columns
#include <stdio.h>
int main() {
int arr[10][10] = {0};
int i = 0, j = 0;
/* The first column is all 1. The i-th row and i-th column are all 1. */
for (i = 0; i < 10; i++) {
arr[i][0] = 1;
arr[i][i] = 1;
}
/* Starting from the third row, the second column arr[2][1] equals the sum of the first column element arr[1][0] and the second column element arr[1][1] (excluding the first and last elements from the third row onwards) */
for (i = 2; i < 10; i++) {
for (j = 1; j <= i; j++) {
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
}
/* Loop through all results */
for (i = 0; i < 10; i++) {
for (j = 0; j <= i; j++) {
printf("%5d", arr[i][j]);
}
printf("\n"); // New line after printing each row
}
return 0;
}
Calculate the sum of 1 + 3 + 5 + … + 99
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i < 100; i += 2) {
sum = sum + i;
}
printf("1 + 3 + 5 + ... + 99 = %d\n", sum);
return 0;
}
Use a function to find the maximum of two numbers
#include <stdio.h>
int getMax(int x, int y) {
/* Pass in 2 parameters to receive */
// int z = x > y ? x : y; // Ternary operator
if (x > y)
return x;
else {
return y;
}
}
int main() {
int a = 10, b = 20;
int max = getMax(a, b);
printf("Maximum value max = %d\n", max);
return 0;
}
Convert uppercase characters in an input line to lowercase
#include <stdio.h>
int main() {
char ch;
while ((ch = getchar()) != '\n') {
if (ch >= 'A' && ch <= 'Z') {
ch = ch + 32; /* ch = ch - 'A' + 'a'; */ // This can also convert uppercase characters to lowercase.
}
putchar(ch);
}
putchar('\n');
return 0;
}
Input two positive integers and find their greatest common divisor using the Euclidean algorithm
#include <stdio.h>
int main() {
int m, n, p = 1, count = 0;
printf("Please enter two positive integers:\n");
scanf_s("%d%d", &m, &n);
while (p != 0) {
p = m % n;
m = n;
n = p;
count++;
}
printf("Greatest common divisor: %d\n", m);
printf("Count: %d\n", count);
return 0;
}
Find the highest and lowest scores among 10 students in an exam
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#define N 10
int main() {
float score[N];
float max_score, min_score;
int i = 0;
for (i = 0; i < N; i++) {
// Input values for all elements through a loop
scanf("%f", &score[i]);
}
max_score = score[0];
min_score = score[0];
for (i = 1; i < N; i++) {
if (score[i] > max_score) {
max_score = score[i];
} else if (score[i] < min_score) {
min_score = score[i];
}
}
printf("Highest score: %f, Lowest score: %f\n", max_score, min_score);
return 0;
}
Write a program to output the sum of factorials from 1 to 10
#include <stdio.h>
int main() {
int ret = 1;
int sum = 0;
for (int n = 1; n <= 10; n++) {
ret = ret * n;
sum = sum + ret;
}
printf("sum = %d\n", sum);
return 0;
}
Find the maximum value among 10 integers
#include <stdio.h>
int main() {
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int max = arr[0]; // Assume the first element is the maximum
int sz = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements in the array
int i = 0;
for (i = 1; i < 10; i++) {
// Only need to compare from the second element
if (arr[i] > max) // Compare each number with max, assign the larger value to max
max = arr[i];
}
printf("max = %d\n", max);
return 0;
}
Recursion: Frog Jumping Stairs Problem – n stairs, can jump 1 or 2 stairs at a time, how many ways to jump to the n-th stair
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int jumpWays(int n) {
if (n <= 1)
return 1; // If the number of stairs is less than or equal to 1, there is only one way to jump directly to that stair
else {
// The number of ways to jump to the n-th stair equals the number of ways to jump to the n-1-th stair plus the number of ways to jump to the n-2-th stair
return jumpWays(n - 1) + jumpWays(n - 2);
}
}
int main() {
int n = 0;
int method = 0;
printf("Please enter the number of stairs:");
scanf("%d", &n);
method = jumpWays(n);
printf("There are %d ways to jump to the %d-th stair!\n", n, method);
return 0;
}
Calculate the sum of squares of all numbers from 1 to 10
#include <stdio.h>
#include <math.h>
int main() {
int i, sum = 0;
for (i = 0; i <= 10; i++) {
sum += i * i;
/* sum += pow(i, 2); */ // If using this statement to calculate the sum of squares, the header file must include #include <math.h>
}
printf("The sum of squares of all numbers from 1 to 10: %d\n", sum);
return 0;
}
Bubble Sort Algorithm
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#define N 10
int main() {
int data[N + 1], i, j, temp;
printf("Please enter %d integers:\n", N);
for (i = 1; i <= N; i++) {
/* Input N integers */
scanf("%d", &data[i]);
}
for (i = 1; i < N; i++) { /* Sort N numbers for N-1 times */
for (j = 1; j <= N - i; j++) {
if (data[j] > data[j + 1]) { /* If the previous number is greater than the next number, swap positions */
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
printf("Sorted in ascending order:\n");
for (i = 1; i <= N; i++) {
printf("%d ", data[i]);
}
printf("\n");
return 0;
}
Calculate the sum of 1 – 1/2 + 1/3 – 1/4 + …. + 1/100
#include <stdio.h>
int main() {
int i;
double sum = 0.0;
int flag = 1; /* Control the sign */
for (i = 1; i < 100; i++) {
sum += flag * 1.0 / i;
flag = -flag;
}
printf("%lf\n", sum);
return 0;
}
Output the factorial of n
#include <stdio.h>
int main() {
int n = 0;
scanf("%d", &n);
int ret = 1;
for (int i = 1; i < n + 1; i++) {
ret = ret * i;
}
printf("ret = %d\n", ret);
return 0;
}
Recursion: Find the n-th Fibonacci number
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h> // Slow efficiency, very slow for large data
int Fib(int n) {
if (n <= 2)
return 1;
else {
return Fib(n - 1) + Fib(n - 2);
}
}
int main() {
int n = 0;
scanf("%d", &n);
int ret = Fib(n);
printf("ret = %d\n", ret);
return 0;
}
Output all Armstrong numbers between 100 and 1000
#include <stdio.h>
int main() {
int i = 0;
int bai, shi, ge;
for (i = 100; i < 1000; i++) {
bai = i / 100; // Get the hundreds place
shi = i / 10 % 10; // Get the tens place
ge = i % 10; // Get the units place
if (i == bai * bai * bai + shi * shi * shi + ge * ge * ge) {
printf("%d\n", i);
}
}
return 0;
}
Print odd numbers from 1 to 100
#include <stdio.h>
int main() {
for (int i = 1; i < 101; i++) {
if (i % 2 == 1) {
printf("%d, ", i);
}
}
return 0;
}
Print leap years between 1000 and 2000, and count the number of leap years
#include <stdio.h>
int main() {
int count = 0; // Count of leap years
for (int i = 1000; i <= 2000; i++) {
// Check if it is a leap year
// 1. A year that is divisible by 4 but not by 100 is a leap year
// 2. A year that is divisible by 400 is a leap year
if (i % 4 == 0 && i % 100 != 0) {
printf("%d ", i);
count++;
} else if (i % 400 == 0) {
printf("%d ", i);
count++;
}
}
printf("\n");
printf("count = %d\n", count);
return 0;
}
Print any day of the week from Monday to Sunday
#include <stdio.h>
int main() {
int day = 0;
scanf_s("%d/", &day);
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Input error");
break;
}
return 0;
}
Print the multiplication table from 1 to 9
#include <stdio.h>
int main() {
int i = 0, j = 0;
for (i = 1; i <= 9; i++) {
for (j = 1; j <= i; j++) {
printf("%d*%d=%-2d ", i, j, i * j);
}
printf("\n");
}
return 0;
}
Input 10 integers from the keyboard and find the maximum number among them
#include <stdio.h>
#define N 10
int main() {
int i, num, max;
printf("Input the first integer:");
scanf_s("%d", &num);
max = num; // Assume the first value is the maximum
for (i = 2; i <= N; i++) {
printf("Input the %d-th integer:", i);
scanf_s("%d", &num);
if (num > max) {
max = num;
printf("Current maximum value: %d\n", max);
}
}
printf("\nMaximum value: %d\n", max);
return 0;
}
Input any 3 numbers from the keyboard and find the smallest number among them
#include <stdio.h>
int main() {
float num1 = 0;
float num2 = 0;
float num3 = 0;
float min;
printf("Input any 3 numbers:\n");
scanf_s("%f%f%f", &num1, &num2, &num3);
min = num1; /* Assume the first number as the minimum */
if (min > num2) {
min = num2;
if (min > num3) {
min = num3;
printf("Minimum value: %f\n", min);
} else {
printf("Minimum value: %f\n", min);
}
} else {
if (min > num3) {
min = num3;
printf("Minimum value: %f\n", min);
} else {
printf("Minimum value: %f\n", min);
}
}
return 0;
}
Reverse the digits of a two-digit number input by the user. For example, if the user inputs 48, the program outputs 84
#include <stdio.h> // Reverse display effect
int f_num(int number) {
int shi = number / 10; /* Get the tens place */
int ge = number % 10; /* Get the units place */
return ge * 10 + shi;
}
int main() {
int number;
printf("Please enter a two-digit number:");
scanf_s("%d", &number);
int reverseNumber = f_num(number);
printf("The reversed number is: %d\n", reverseNumber);
return 0;
}
Recursion: Tower of Hanoi Problem
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
void hanoi(int n, char source, char target, char middle) {
if (n == 1) // When there is only one disk, move it directly from the source pole to the target pole
printf("Move disk from %c to %c\n", source, target);
else {
// Recursively move n-1 disks from the source pole to the auxiliary pole via the target pole
hanoi(n - 1, source, middle, target);
// Move the largest disk from the source pole to the target pole
printf("Move disk from %c to %c\n", source, target);
// Move n-1 disks from the auxiliary pole to the target pole via the source pole
hanoi(n - 1, middle, target, source);
}
}
int main() {
int n = 0;
printf("Please enter the number of disks:");
scanf("%d", &n);
hanoi(n, 'A', 'B', 'C');
return 0;
}
Finally, I wish everyone to have a positive and enterprising attitude towards learning.If you are currently learning C language, please vote on the difficulty of C language from your personal perspective! There are quite a few questions in the above repository. If you have learned or are tired, we should balance work and rest to learn more effectively, and the efficiency and absorption of learning are very high. I found a very fun game that helps our brain think. If you are interested, you can click below to try it!
Please open in the WeChat client