C语言十道入门级程序

标题C语言十道简单入门程序题``

备注:该程序在VS2013环境下都能运行,在其他环境下可能因为语法问题可能运行不了。如有问题可私信QQ:1835783944

第一题:输入一行字符,分别统计出其中英文字母、空格、数字、其它字符的个数!利用while语句。

#include "stdio.h"

main()

{

  char c;

  int letters=0,space=0,digit=0,others=0;

  printf("please input some characters\n");

  while((c=getchar())!='\n')

  {

    if(c>='a'&&c<='z'||c>='A'&&c<='Z')

      letters++;

      else if(c==' ')

        space++;

        else if(c>='0'&&c<='9')

          digit++;

        else

          others++;

  }

  printf("all in all:char=%d space=%d digit=%d others=%d\n",letters, space,digit,others);

}

第二题:输入一个5位数,判断它是不是回文,如12321即是回文数。

#include "stdio.h"

main( )

{

  long ge,shi,qian,wan,x;

  scanf("%ld",&x);

  wan=x/10000;

  qian=x%10000/1000;

  shi=x%100/10;

  ge=x%10;

  if(ge==wan&&shi==qian)/*个位等于万位并且十位等于千位*/

    printf("this number is a huiwen\n");

  else

    printf("this number is not a huiwen\n");

}

第三题:利用递归方法求10!

#include"stdio.h"

void main()

{

int n;

n = jiechen(10);//调用函数jiechen求10的阶乘,并将结果返回给n;

printf("%d",n);

}

 

int jiechen(int n)//函数

{

if (n > 1)

{

n = n * jiechen(n-1);

}

else n = 1;

return n;

}

第四题:求100之内的素数。

#include"stdio.h"

main()

{

int i,j,count=0;

for (i = 2; i <= 100; i++)

{

for (j = 1; j < i; j++)

{

if (i%j == 0)

count++;

if (count>1)

break;

}

if (count == 1)

printf("%d ",i);

count = 0;

}

system("pause");

}

第五题:对10个数进行排序。

#include"stdio.h"

void main()

{

printf("请输入10个整数");

int a[10] = { 0 };

int i,j,min,tmp;

for (i = 0; i < 10; i++)

{

scanf("%d",&a[i]);

}

for (i = 0; i <= 8; i++)

{

min = i;

for (j = i + 1; j <= 9; j++)

{

if (a[j]<a[min])

min = j;

}

tmp = a[i];

a[i] = a[min];

a[min] = tmp;

}

for (i = 0; i < 10; i++)

{

printf("%d ",a[i]);

}

}



第六题:求一个二维矩阵的转置矩阵,请使用二维数组来描述。

#include"stdio.h"

void main()

{

int a[4][4] = { 0 };

int b[4][4] = { 0 };

int i, j;

printf("请输入一个4*4的整数矩阵:\n");

for (i = 0; i < 4; i++)

{

for (j = 0; j < 4; j++)

{

scanf("%d",&a[i][j]);

}

}

for (i = 0; i < 4; i++)

{

for (j = 0; j < 4; j++)

{

b[j][i] = a[i][j];

}

}

for (i = 0; i < 4; i++)

{

for (j = 0; j < 4; j++)

{

printf("%d ",b[i][j]);

}

printf("\n");

}

}

第七题:打印出杨辉三角。

#include<stdio.h>

int main()

{

printYangHui();//

return 0;

}

int printYangHui()//按行数打印杨辉三角

{

int rows, coef = 1, space, i, j;

printf("行数: ");

scanf("%d", &rows);

 

//控制行数

for (i = 0; i<rows; i++)

{

//打印空格

for (space = 1; space <= rows - i; space++)

printf("  ");

//

for (j = 0; j <= i; j++)//第n行的数字有n项,所以j<=i

{

//第一行为1,第一列为1

if (j == 0 || i == 0)

coef = 1;

else

coef = coef*(i - j + 1) / j;//最后一个每一行i=j,倒数第二行(coef=j)等于列数,

printf("%4d", coef);

}

printf("\n");

}

}


第八题:求一个字符串的长度。

#include<string.h>

void main()

{

char a[100];

int i = 0;

printf("input string\n");

gets(a);

while (a[i] != '\0')

i++;

printf("你输入的字符串的长度为%d\n", i);

}

第九题:一维数组中元素的最大值和下标。


#include"stdio.h"

void main()

{

int a[] = { 1, 4, 7, 4, 8, 5, 2, 9, 1000 };//一共9个数,最大值为1000,其下标为8

int i, max;

max = 0;

for (i = 1; i < 9; i++)

{

if (a[max] < a[i])

max = i;

}

printf("最大值 = %d\n下标为 = %d\n", a[max], max);

}

第十题:用递归函数反序输出字符串,如abcdefg,反序输出为gfedcba。

VS2013环境下的代码。用其他编程软件运行可能会出现错误,稍微改改就好了。

#include"stdio.h"

void ret(char *str)

{

if (*str != '\0')

{

ret(str + 1);

}

 

printf("%c ", *str);

}

int main()

{

char a[20];

scanf_s("%s",a,20);

ret(a);

printf("\n运行成功!\n");

system("pause");

}```
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值