1>
要求:
源代码:
#include <stdio.h>
int fun(int n)
{if(n==0) return 1;return n*fun(n-1);
}
int main(int argc, char const *argv[])
{/* code */int n;printf("enter n :");scanf("%d",&n);int s=fun(n);printf("n ! = %d\n" ,s);return 0;
}
效果图:
2>
要求:
源代码:
#include <stdio.h>
int fun(int n)
{if(n==0) return 0;return n+fun(n-1);
}
int main(int argc, char const *argv[])
{/* code */int n;printf("enter n :");scanf("%d",&n);int s=fun(n);printf("sum = %d\n" ,s);return 0;
}
效果图:
3>
要求:
源代码:
#include <stdio.h>
int fun(int n)
{if(n==1||n==0) return 1;else return fun(n-1)+fun(n-2);}
int main(int argc, char const *argv[])
{int n;printf("n= :");scanf("%d",&n);for (int i = 1; i <=n; i++){printf("%-3d",fun(i) );}puts("");fun(n);return 0;
}
效果图:
4>
要求:
源代码:
#include <stdio.h>
void fun(int n)
{int r=n%2;if (n >= 2)fun(n / 2);putchar('0' + r);return;
}
int main(int argc, char const *argv[])
{/* code */int n;printf("enter n :");scanf("%d",&n);fun(n);puts("");return 0;
}
效果图:
5>
要求:
源代码:
#include <stdio.h>
int fun(int n)
{if(n/10==0) return n;return n%10+fun(n/10);
}
int main(int argc, char const *argv[])
{/* code */int n;printf("enter n :");scanf("%d",&n);int s=fun(n);printf("sum = %d\n" ,s);return 0;
}
效果图:
6>
要求:
源代码:
#include <stdio.h>
int fun(int n)
{if(n/10==0) return n;return n%10*fun(n/10);
}
int main(int argc, char const *argv[])
{/* code */int n;printf("enter n :");scanf("%d",&n);int s=fun(n);printf("mul = %d\n" ,s);return 0;
}
效果图:
7>
要求:
源代码:
#include <stdio.h>
#include<string.h>
void fun(char *p,int i,int j)
{char t;if(i==j)return;else{t=*(p+i);*(p+i)=*(p+j);*(p+j)=t;return fun(p,i+1,j-1);}
}
int main(int argc, char const *argv[])
{char str[20];printf("please enter str:");gets(str);int len=strlen(str)-1;fun(str,0,len);puts(str);return 0;
}
效果图: