[Regaining C Language] 4. Collection of classic examples of loop programming (greatest common factor, factorial sum, positive integer translation, printing character square matrix, Fibonacci sequence...)

Table of contents

Preface

4. Cycle programming

4.3 Programming examples

4.3.1 Find the greatest common factor of two numbers

4.3.2 Factorial summation

4.3.3 Positive integer translation

4.3.4 Print character matrix

4.3.5 Hundred Money and Hundred Chickens Problem 

4.3.6 Fibonacci Sequence

4.3.7 Iterative method to solve equations


Preface

ChatGPT

        C is a general-purpose, procedural computer programming language developed by Dennis Ritchie at Bell Labs in the early 1970s. It is designed to be a simple, efficient and portable language for system-level programming and application development.

The following are some of the main features of C language:

1. Structured programming : C language supports structured programming, which provides sequential execution, conditional statements (such as if-else and switch) and loop structures (such as for and while), making the program easier to read, understand and maintain.

2. Intermediate language : C language has the characteristics of both high-level language and low-level language. It provides rich control structures and operators, can directly access memory and hardware, and allows programmers to perform low-level operations, such as bit operations and pointer operations.

3. Portability : Programs written in C language are highly portable because its syntax and features remain basically the same on different computer systems. This allows programs to be compiled and executed on different platforms without requiring too many modifications to the code.

4. Powerful standard library : C language provides a rich standard library, which contains various functions and tools for processing input and output, string operations, memory management, mathematical operations, etc. These functions can improve the efficiency of program development and can be used across platforms.

5. System-oriented programming : Because C language has the ability to directly access memory and hardware, it is often used to develop underlying software such as operating systems, embedded systems, and drivers. The C language also provides features such as bit operations and pointers, allowing programmers to better control system resources.

6. Extensibility : C language allows programmers to write custom functions and libraries to extend the functionality of the language. This makes the C language very suitable for the development of large projects, where code can be organized into a modular structure and reused through function calls.

        Although the C language is relatively low-level, it is still a very important and widely used programming language. It provides programmers with powerful control capabilities and performance advantages, and has been widely used in various fields, such as system programming, embedded development, game development, etc. Many modern programming languages, such as C++, Java, and Python, have been influenced by the C language and have been extended and improved on it.

4. Cycle programming

4.3 Programming examples

4.3.1 Find the greatest common factor of two numbers

#include <stdio.h>

int main() {
    int num1, num2;
    printf("请输入两个整数:");
    scanf("%d %d", &num1, &num2);

    int gcd;
    for (int i = 1; i <= num1 && i <= num2; i++) {
        if (num1 % i == 0 && num2 % i == 0) {
            gcd = i;
        }
    }

    printf("最大公因数是:%d\n", gcd);

    return 0;
}

4.3.2 Factorial summation

       Use a loop to calculate the factorial of a series of numbers and sum them.

#include <stdio.h>

int main() {
    int n;
    printf("请输入一个正整数:");
    scanf("%d", &n);

    int factorial = 1;
    int sum = 0;

    for (int i = 1; i <= n; i++) {
        factorial *= i;
        sum += factorial;
    }

    printf("阶乘和为:%d\n", sum);

    return 0;
}

4.3.3 Positive integer translation

        Use a loop to translate a positive integer into its corresponding string representation.

4.3.4 Print character matrix

#include <stdio.h>

int main() {
    int size;
    printf("请输入方阵的大小:");
    scanf("%d", &size);

    if (size <= 0) {
        printf("无效的大小!请输入一个正整数。\n");
        return 0;
    }

    char character;
    printf("请输入要打印的字符:");
    scanf(" %c", &character);

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            printf("%c ", character);
        }
        printf("\n");
    }

    return 0;
}

4.3.5 Hundred Money and Hundred Chickens Problem 

        The problem of hundreds of coins and hundreds of chickens is a classic mathematical problem that can be solved by the exhaustive method. The description of the problem is as follows: Suppose there are 100 coins and 100 chickens. Each rooster costs 5 coins, each hen costs 3 coins, and three chickens cost 1 coin. Now I want to buy 100 chickens for 100 money. How many roosters, hens and chicks are there?

#include <stdio.h>

int main() {
    printf("公鸡数量\t母鸡数量\t小鸡数量\n");

    for (int x = 0; x <= 20; x++) {
        for (int y = 0; y <= 33; y++) {
            int z = 100 - x - y;
            if (5 * x + 3 * y + z / 3 == 100 && z % 3 == 0) {
                printf("%d\t\t%d\t\t%d\n", x, y, z);
            }
        }
    }

    return 0;
}

4.3.6 Fibonacci Sequence

#include <stdio.h>

int main() {
    int n = 10; // 需要计算斐波那契数列的项数
    int a = 0, b = 1, temp, i;

    printf("斐波那契数列前 %d 项:\n", n);
    printf("%d %d ", a, b);

    for (i = 2; i < n; i++) {
        temp = a + b;
        printf("%d ", temp);
        a = b;
        b = temp;
    }
    printf("\n");

    return 0;
}

4.3.7 Iterative method to solve equations

Guess you like

Origin blog.csdn.net/m0_63834988/article/details/133557191