advertisements
السلام عليكم ورحمة الله وبركاته
اليوم بإذن الله سنتعرف على الدوال العكسية
والفائدة منها
مسودة الدرس :
الهكلية العامة :
void recursion() { /* function calls itself */ recursion(); } int main() { recursion(); }
تمرين Factorial :
#include "stdio.h" /* Find power of a number using recursion using c program */ int main(){ int pow,num; long int res; long int power(int,int); printf("\nEnter a number: "); scanf("%d",&num); printf("\nEnter power: "); scanf("%d",&pow); res=power(num,pow); printf("\n%d to the power %d is: %ld",num,pow,res); return 0; } int i=1; long int sum=1; long int power(int num,int pow){ if(i <= pow){ sum=sum*num; power(num,pow-1); } else return sum; }
تمرين حساب عدد بالأس :
#include "stdio.h" int factorial(unsigned int i) { if(i <= 1) { return 1; } return i * factorial(i - 1); } int main() { int i = 3; printf("Factorial of %d is %d\n", i, factorial(i)); return 0; }
تمرين حساب الضرب :
#include/* C code to multiply two numbers by recursion: */ int multiply(int,int); int main(){ int a,b,product; printf("Enter any two integers: "); scanf("%d%d",&a,&b); product = multiply(a,b); printf("Multiplication of two integers is %d",product); return 0; } int multiply(int a,int b){ static int product=0,i=0; if(i < a){ product = product + b; i++; multiply(a,b); } return product; }
فديو الشرح :
في أمان الله
0 commentaires :
Publier un commentaire