int sum(int n)
{
if(n<=0)
return 0;
else
n= n+sum(n-1);
return n;
}
void main(){
int n = 10;
int summ;
summ = sum(n);
printf("sum = %d\n",summ);
}
.........................
factorial
#include
int factorial(int n)
{
if(n<=0)
return 1;
else
n= n*factorial(n-1);
return n;
}
void main(){
int n = 5;
int summ;
summ = factorial(n);
printf("sum = %d\n",summ);
}
................................
max min!
#include
void minmax(int *listnumber,int n,int *p_min,int *p_max){
int min2,max2;
if(n==1)
*p_max = *p_min = listnumber[0];
else if(n==2)
{
if(listnumber[0]
*p_max = listnumber[1];
}else{
*p_min = listnumber[1];
*p_max = listnumber[0];
}
}else{
minmax(listnumber,n/2,p_min,p_max);
minmax(listnumber+n/2,n-(n/2),&min2,&max2);
if(min2<*p_min)
*p_min = min2;
if(max2>*p_max)
*p_max = max2;
}
}
void main(){
int list[10]={1,5,7,6,3,4,8,9,2,0} ;
int p_max,p_min;
int n=10;
minmax(list,n,&p_min,&p_max);
printf("max = %d : min = %d\n",p_max,p_min);
}
No comments:
Post a Comment