中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

c語言10個經典小程序

小億
92
2024-03-08 14:43:34
欄目: 云計算

  1. Hello World
#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
  1. 計算兩數之和
#include <stdio.h>

int main() {
    int num1 = 5, num2 = 10;
    int sum = num1 + num2;

    printf("The sum of %d and %d is %d\n", num1, num2, sum);

    return 0;
}
  1. 判斷奇偶數
#include <stdio.h>

int main() {
    int num = 6;

    if(num % 2 == 0) {
        printf("%d is an even number\n", num);
    } else {
        printf("%d is an odd number\n", num);
    }

    return 0;
}
  1. 找出最大數
#include <stdio.h>

int main() {
    int num1 = 10, num2 = 20, num3 = 15;
    int max;

    max = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);

    printf("The largest number is %d\n", max);

    return 0;
}
  1. 計算階乘
#include <stdio.h>

int main() {
    int num = 5;
    int factorial = 1;

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

    printf("Factorial of %d is %d\n", num, factorial);

    return 0;
}
  1. 判斷素數
#include <stdio.h>

int main() {
    int num = 7;
    int isPrime = 1;

    for(int i = 2; i <= num / 2; i++) {
        if(num % i == 0) {
            isPrime = 0;
            break;
        }
    }

    if(isPrime) {
        printf("%d is a prime number\n", num);
    } else {
        printf("%d is not a prime number\n", num);
    }

    return 0;
}
  1. 反轉數組
#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Original array: ");
    for(int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\nReversed array: ");
    for(int i = n-1; i >= 0; i--) {
        printf("%d ", arr[i]);
    }

    return 0;
}
  1. 斐波那契數列
#include <stdio.h>

int main() {
    int n = 10;
    int first = 0, second = 1, next;

    for(int i = 0; i < n; i++) {
        if(i <= 1) {
            next = i;
        } else {
            next = first + second;
            first = second;
            second = next;
        }

        printf("%d ", next);
    }

    return 0;
}
  1. 冒泡排序
#include <stdio.h>

void bubbleSort(int arr[], int n) {
    for(int i = 0; i < n-1; i++) {
        for(int j = 0; j < n-i-1; j++) {
            if(arr[j] > arr[j+1]) {
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr) / sizeof(arr[0]);

    bubbleSort(arr, n);

    printf("Sorted array: ");
    for(int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}
  1. 矩陣乘法
#include <stdio.h>

int main() {
    int mat1[2][2] = {{1, 2}, {3, 4}};
    int mat2[2][2] = {{5, 6}, {7, 8}};
    int res[2][2];

    for(int i = 0; i <

0
东源县| 瓦房店市| 罗田县| 阿合奇县| 中江县| 云龙县| 孟州市| 紫云| 铜梁县| 宁海县| 旬邑县| 如东县| 镇远县| 同仁县| 正蓝旗| 泰宁县| 吴川市| 沅江市| 南木林县| 浙江省| 横峰县| 太仓市| 泰兴市| 桦甸市| 石景山区| 平湖市| 西藏| 天峻县| 嘉禾县| 丰城市| 科尔| 萝北县| 洱源县| 漠河县| 贞丰县| 泉州市| 雷山县| 容城县| 新邵县| 共和县| 南皮县|