1、计算Fibonacci数列
Fibonacci数列又称斐波那契数列,又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21。
C语言实现的代码如下:

 /* t3ing Fibonacci series up to certain number entered by user. */
 #include <stdio.h>
 int main()
 {
   , t2=, t3=, num, sum=;
   printf("Enter an integer: ");
   scanf("%d",&num);
   printf("Fibonacci Series: %d,%d,",t1,t2); /* t3ing first two terms */
   t3=t1+t2;
   while(t3<num)
   {
       sum=sum+t3;
       printf("%d,",t3);
       t1=t2;
       t2=t3;
       t3=t1+t2;
   }
   printf("\nsum=%d\n",sum);
   ;
 }

Fibonacci.c

2、回文检查

 //用数组
 #include <stdio.h>
 void main()
 {
     ];
     ,j=;
     printf("Please input string:");
     gets(a);
     while(a[i]!='\0')
         i++;
     i--;
     for(;j<=i;i--,j++)
     {
         if(a[i]!=a[j])
             break;
     }
     if(j<=i)
         printf("%s is not a palindrome\n",a);
     else
         printf("%s is a palindrome\n",a);
 }

 //用指针
 #include<stdio.h>
 int hw(char a[])
 {
     char *p,*q;
     p=q=a;
     while(*q!='\0'){
         q++;
     }
     q--;
     while(p<q)
     {
         if(*p==*q){
             p++;
             q--;
         }
         else
             ;
     }
     ;
 }
 int main()
 {
     ];
     gets(a);
     if(hw(a))
         printf("是回文!\n");
     else
         printf("不是回文!\n");
 }

hw.c

3、质数检查
注:1既不是质数也不是合数。

 /* C program to check whether a number is prime or not. */
 #include <stdio.h>
 int main()
 {
   ;
   printf("Enter a positive integer: ");
   scanf("%d",&n);
   ;i<=n/;++i)
   {
       )
       {
           flag=;
           break;
       }
   }
   )
       printf("%d is a prime number.",n);
   else
       printf("%d is not a prime number.",n);
   ;
 }

check.c

4、打印金字塔和三角形
使用 * 建立三角形

 #include <stdio.h>
 int main()
 {
     int i,j,rows;
     printf("Enter the number of rows: ");
     scanf("%d",&rows);
     ;i<=rows;++i)
     {
         ;j<=i;++j)
         {
            printf("%d ",j);
         }
         printf("\n");
     }
     ;
 }

triangle.c

 #include <stdio.h>
 int main()
 {
     ;
     printf("Enter the number of rows: ");
     scanf("%d",&rows);
     ;i<=rows;++i)
     {
         ;space<=rows-i;++space)
         {
            printf("  ");
         }
         *i-)
         {
            printf("* ");
            ++k;
         }
         k=;
         printf("\n");
     }
     ;
 }

pyramid.c

5、简单的加减乘除计算器
源代码:

 /* Source code to create a simple calculator for addition, subtraction, multiplication and division using switch...case statement in C programming. */
 # include <stdio.h>
 int main()
 {
     char op;
     float num1,num2;
     printf("Enter operator either + or - or * or divide : ");
     scanf("%c",&op);
     printf("Enter two operands: ");
     scanf("%f%f",&num1,&num2);
     switch(op) {
         case '+':
             printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
             break;
         case '-':
             printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
             break;
         case '*':
             printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
             break;
         case '/':
             printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
             break;
         default:
             /* If operator is other than +, -, * or /, error message is shown */
             printf("Error! operator is not correct");
             break;
     }
     ;
 }

calculator.c

6、检查一个数能不能表示成两个质数之和
源代码:

 #include <stdio.h>
 int prime(int n);
 int main()
 {
     ;
     printf("Enter a positive integer: ");
     scanf("%d",&n);
     ; i<=n/; ++i)
     {
         )
         {
             )
             {
                 printf("%d = %d + %d\n", n, i, n-i);
                 flag=;
             }

         }
     }
     )
       printf("%d can't be expressed as sum of two prime numbers.",n);
     ;
 }
 int prime(int n)      /* Function to check prime number */
 {
     ;
     ; i<=n/; ++i)
        )
           flag=;
     return flag;
 }

6.c

7、用递归的方式颠倒字符串
源代码:

 /* Example to reverse a sentence entered by user without using strings. */
 #include <stdio.h>
 void Reverse();
 int main()
 {
     printf("Enter a sentence: ");
     Reverse();
     ;
 }
 void Reverse()
 {
     char c;
     scanf("%c",&c);
     if( c != '\n')
     {
         Reverse();
         printf("%c",c);
     }
 }

Reverse.c

8、实现二进制与十进制之间的相互转换

 /* C programming source code to convert either binary to decimal or decimal to binary according to data entered by user. */
 #include <stdio.h>
 #include <math.h>
 int binary_decimal(int n);
 int decimal_binary(int n);
 int main()
 {
    int n;
    char c;
    printf("Instructions:\n");
    printf("1. Enter alphabet 'd' to convert binary to decimal.\n");
    printf("2. Enter alphabet 'b' to convert decimal to binary.\n");
    scanf("%c",&c);
    if (c =='d' || c == 'D')
    {
        printf("Enter a binary number: ");
        scanf("%d", &n);
        printf("%d in binary = %d in decimal", n, binary_decimal(n));
    }
    if (c =='b' || c == 'B')
    {
        printf("Enter a decimal number: ");
        scanf("%d", &n);
        printf("%d in decimal = %d in binary", n, decimal_binary(n));
    }
    ;
 }

 int binary_decimal(int n) /* Function to convert binary to decimal.*/
 {
     , i=, rem;
     )
     {
         rem = n%;
         n/=;
         ,i);
         ++i;
     }
     return decimal;
 }

 int decimal_binary(int n)  /* Function to convert decimal to binary.*/
 {
     , binary=;
     )
     {
         rem=n%;
         n/=;
         binary+=rem*i;
         i*=;
     }
     return binary;
 }

decimal_binary.c

9、使用多维数组实现两个矩阵的相加
源代码:

 #include <stdio.h>
 int main(){
     ][],b[][],sum[][],i,j;
     printf("Enter number of rows (between 1 and 100): ");
     scanf("%d",&r);
     printf("Enter number of columns (between 1 and 100): ");
     scanf("%d",&c);
     printf("\nEnter elements of 1st matrix:\n");

     /* Storing elements of first matrix entered by user. */
     ;i<r;++i)
        ;j<c;++j)
        {
            printf(,j+);
            scanf("%d",&a[i][j]);
        }

     /* Storing elements of second matrix entered by user. */
     printf("Enter elements of 2nd matrix:\n");
     ;i<r;++i)
        ;j<c;++j)
        {
            printf(,j+);
            scanf("%d",&b[i][j]);
        }

     /*Adding Two matrices */
    ;i<r;++i)
        ;j<c;++j)
            sum[i][j]=a[i][j]+b[i][j];

     /* Displaying the resultant sum matrix. */
     printf("\nSum of two matrix is: \n\n");
     ;i<r;++i)
        ;j<c;++j)
        {
            printf("%d   ",sum[i][j]);
            )
                printf("\n\n");
        }
     ;
 }

arry.c

10、矩阵转置
源代码:

 #include <stdio.h>
 int main()
 {
     ][], trans[][], r, c, i, j;
     printf("Enter rows and column of matrix: ");
     scanf("%d %d", &r, &c);

     /* Storing element of matrix entered by user in array a[][]. */
     printf("\nEnter elements of matrix:\n");
     ; i<r; ++i)
     ; j<c; ++j)
     {
         printf(,j+);
         scanf("%d",&a[i][j]);
     }
     /* Displaying the matrix a[][] */
     printf("\nEntered Matrix: \n");
     ; i<r; ++i)
     ; j<c; ++j)
     {
         printf("%d  ",a[i][j]);
         )
             printf("\n\n");
     }

     /* Finding transpose of matrix a[][] and storing it in array trans[][]. */
     ; i<r; ++i)
     ; j<c; ++j)
     {
        trans[j][i]=a[i][j];
     }

     /* Displaying the transpose,i.e, Displaying array trans[][]. */
     printf("\nTranspose of Matrix:\n");
     ; i<c; ++i)
     ; j<r; ++j)
     {
         printf("%d  ",trans[i][j]);
         )
             printf("\n\n");
     }
     ;
 }

resarry.c

10个C语言经典的更多相关文章

  1. 【转载】经典10道c/c++语言经典笔试题(含全部所有参考答案)

    经典10道c/c++语言经典笔试题(含全部所有参考答案) 1. 下面这段代码的输出是多少(在32位机上). char *p; char *q[20]; char *m[20][20]; int (*n ...

  2. C语言经典例题100

    C语言经典例题100 来源 http://www.fishc.com 适合初学者 ----------------------------------------------------------- ...

  3. C语言经典算法 - 多维矩阵转一维矩阵的代码

    下边内容内容是关于C语言经典算法 - 多维矩阵转一维矩阵的内容,应该能对码农也有好处. #include <stdio.h>#include <stdlib.h>int mai ...

  4. C语言经典例题(菜鸟教程100例)

    学习c语言基础,怎么能少了菜鸟教程上的100道例题呢,这里整理一下每道题的链接,希望大家能享受学习的乐趣 1,有1,2,3,4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 2,企业发放 ...

  5. C 语言经典100例

    C 语言经典100例 C 语言练习实例1 C 语言练习实例2 C 语言练习实例3 C 语言练习实例4 C 语言练习实例5 C 语言练习实例6 C 语言练习实例7 C 语言练习实例8 C 语言练习实例9 ...

  6. C语言经典100例(51-100)

    [程序51] 题目:学习使用按位与 & . 分析:0&0=0; 0&1=0; 1&0=0; 1&1=1 #include "stdio.h" ...

  7. C语言经典100例(1-50)

    [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去掉不满足条件的排列. main ...

  8. 猴子吃桃问题之《C语言经典案例分析》

    猴子吃桃问题之<C语言经典案例分析>一.[什么是猴子吃桃]       猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个.第二天早上又将第一天剩下的桃子吃掉一半 ...

  9. C语言经典100例-ex001

    系列文章<C语言经典100例>持续创作中,欢迎大家的关注和支持. 喜欢的同学记得点赞.转发.收藏哦- 后续C语言经典100例将会以pdf和代码的形式发放到公众号 欢迎关注:计算广告生态 即 ...

随机推荐

  1. 有关numpy.random下的API具体含义

    1.numpy.random.random(size=None) Return random floats in the half-open interval [0.0, 1.0). 返回size大小 ...

  2. Python map,reduce,filter,apply

    map(function, iterable, ...) map()函数接收两个参数,一个是函数,一个是可迭代的对象,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 基本等 ...

  3. 【Flask】下载多个文件

    使用zipfile模块可以将多个文件打包成zip文件进行下载,但是常规的操作方式会在服务器磁盘上生成一个zip文件占用磁盘空间. 后引入BytesIO将文件写入到内存中然后下载: def dl_pla ...

  4. 第二十三篇、IO多路复用 二

    基于IO多路复用实现socket的连接 下面流程:1.先创建列表,让自己的socket对象传入,然后遍历select第一个参数2.客户端连接,那么客户端socket就传入到了列表里面,3.遍历,接收客 ...

  5. 国内ADSL用户的带宽一般都是1M、2M、3M的,理论上的下载速度分别是128K/S、256K/S、384K/S。

    国内ADSL用户的带宽一般都是1M.2M.3M的,理论上的下载速度分别是128K/S.256K/S.384K/S. 1024/8===128      2048/8==256

  6. 算法(Algorithms)第4版 练习 1.5.3

    id数组和treesize数组变化情况: 0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 10 components 9 0 1 2 3 4 5 6 7 8 9 1 1 ...

  7. HDU 2089 不要62:数位dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 问你在区间[n,m]中,有多少个数字不含"4"且不含"62 ...

  8. mvc购物车项目(2)

    为了避免数据冗余,我们可以把共同的信息,抽出建立一个单独的表,把不是共有的信息,建立一张单独表. 订单表分为两个表 create table orders( id number primary key ...

  9. hdu3572线性欧拉筛

    用线性筛来筛,复杂度O(n) #include<bits/stdc++.h> #include<ext/rope> #define fi first #define se se ...

  10. pyget-资源与标签(Sprite、Label、Font)

    Sprite精灵,是游戏中对具有动画作用功能的图片的爱称. 精灵,图标和字体等资源是不能够处理事件的,因为它们并不是继承自EventDispatcher.但是可以包含一个能够EventDispatch ...