91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
饶河县| 仪征市| 台中县| 孙吴县| 常熟市| 永济市| 达尔| 安庆市| 庆城县| 博乐市| 郸城县| 定南县| 崇州市| 萝北县| 方城县| 蓬溪县| 讷河市| 祁门县| 安多县| 秭归县| 黄陵县| 迁安市| 棋牌| 塘沽区| 沈阳市| 大田县| 贡嘎县| 鲁甸县| 聂拉木县| 武强县| 大石桥市| 临西县| 新乐市| 雷波县| 东源县| 南昌县| 辽中县| 宜良县| 长岛县| 高阳县| 山东省|