1 求阶乘
1 #include <stdio.h> 2 int fact(int n); 3 int main() 4 { 5 int i; 6 printf("请输入要求阶乘的一个整数:"); 7 scanf("%d",&i); 8 printf("%d的阶乘结果为:%d\n",i,fact(i)); 9 getch(); 10 return 0; 11 } 12 int fact(int n) 13 { 14 if(n<=1) 15 return 1; 16 else 17 return n*fact(n-1); 18 }
2 进制转换
1 #include <stdio.h> 2 #include <string.h> 3 void convto(char *s, int n, int b) 4 { 5 char bit[]={"0123456789ABCDEF"}; 6 int len; 7 if(n==0) 8 { 9 strcpy(s,""); 10 return; 11 } 12 convto(s, n/b, b); 13 len = strlen(s); 14 s[len] = bit[n%b]; 15 s[len+1] = ‘\0‘; 16 } 17 18 void main(void) 19 { 20 char s[80]; 21 int i, base,old; 22 printf("请输入十进制数:"); 23 scanf("%d",&old); 24 printf("请输入转换的进制:"); 25 scanf("%d", &base); 26 convto(s, old, base); 27 printf("%s\n", s); 28 getch(); 29 return; 30 }
时间: 2024-11-07 17:09:46