C语言学习过程记录12 函数递归

1.输入一个无符号整数,从后往前分别打印它每位的数,如输入1234,输出4 3 2 1

#include<stdio.h>
void print(int n)
    {
        printf("%2d",n%10);
        if(n>9)
        {
            print(n/10);
        }

    }

int main()
{
    int num = 0;
    scanf("%d",&num);
    print(num);

    return 0;
}

2.计算n的阶乘

#include<stdio.h>

int test( int num)
{
    int re = 1;
    if(num > 0)
    re=num*test(num -1);
    return re;


}
int main()
{
    int n = 0;
    int res = 0;
    scanf("%d",&n);
    res = test(n);
    printf("%d\n",res);
    return 0;
}

3.统计字符数

#include <stdio.h>

int test(char *str)
{
    int res = 0;
    if(*str !='\0')
        res = 1+test(str+1);
    return res;


}
int main()
{
    char arr[1000];
    int n = 0;
    gets(arr);
    n = test(arr);
    printf("字符数等于%d",n);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值