bycj.net
当前位置:首页 >> 用递归函数求5 >>

用递归函数求5

#include<stdio.h> int f(int n) { int s=0; if(n==1) s=1; else s=n*f(n-1); return(s); } void main() { printf("%d",f(5)); }

#include <stdio.h>unsigned long fac(unsigned long n);int main(void){ printf("%lu\n",fac(5)); return 0;}unsigned long fac(unsigned long n){ if(n<2) { return 1; } return n*fac(n-1);}

#include "stdio.h" long f(long n) { if(n==0||n==1) return 1; else return n*f(n-1); } void main() { printf("5!=%ld\n",f(5)); }

度public class fac{ public int f(int val){ if(val==0) return 1; return val*f(val-1); } public static void main(String args[]){ System.out.println((new fac()).f(5)); } }

int sm=1 for(int i=1;i<=5;i++) { sm*=i; } printf("%d",sm);

using System;namespace TestCs{ class Program { public static void Main(string[] args) { //调用函数,并输出 Console.WriteLine(Factorial(5)); // TODO: Implement Functionality Here Console.Write("Press any key to continue . . . "); Console.

把else去掉,让它也返回s,否则主函数调用mypow(a,n),只要n不为0,是没有返回值的.程序修改如下:#include<stdio.h> int main() { double mypow(double,int); double a=5.0; int n=5; printf("5.0的5次方=%f\n",mypow(a,n)); return 0; } double mypow(double x,int y) { static double s=1.0; s*=x; y--; if(y!=0) mypow(x,y); return s; }

上面是c语言的递归

float fac(int n){ float f; if(n 评论0 0 0

Public Function s(n As Integer) As Long If n = 1 Then s = 1 Else s=n * s(n-1) End If End Function Private Sub Form_Click() Print “s(5)=”;s(5) End Sub

网站首页 | 网站地图
All rights reserved Powered by www.bycj.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com