🔥Finding it Damn Hard to Understand C Programming? Learn to code-the right way-with interactive lessons, quizzes & challenges. Build a strong programming base; it's IMPORTANT! Try Programiz PRO for Free: bit.ly/master-c-programming
#include int sum(int n); int main(){ int number; printf("enter a number :"); scanf("%d",&number); int result = sum(number); printf("the answer is %d ",result); return 0; } int sum (int n){ if(n>0){return n*sum(n-1);} else {return 1;} }
Q. Which of the following statements is false? A. A function that calls itself is called recursive function B. Conditional statement prevents infinite recursive call C. Recursive Function always return some value D. None of the above
I do not understand where the second number in the sums in the image come from. For example, in the first one where does the6 in (4 + 6 = 10) come from?
@@testingsomething5280 It comes from the iteration below: 3+3 =6, in which the second 3 comes from the other iteration: 2+1=3, and the 1 comes from yet another iteration: 1+0=1. Everything together is the same as 4+3+2+1=10
/*Create a program that computes the factorial of a number. • Take input from user • Pass the input value to a function. • Inside the function check if the number is greater than O. • If true, return number multiplied by a recursive call to the function with parameter 1 less than number Otherwise, return 1. */ int factorial(int n) { if (n != 0) { if (n < 1) return 1; return n * factorial(n - 1); }else return 1; }
I've recycled the code of the previous exercise and I've changed the + on the 20th line with * #include int factorial(int n); int main() { int number,result; printf("Enter a number: "); scanf("%d", &number); result = factorial(number); printf("Factorial number = %d", result); return 0; } int factorial(int n){ if (n > 0){ return n * factorial(n-1); } else { return 1; } }
Task: #include #include int mult(int n); int main() { int number, result; printf("enter a number: "); scanf("%d ", &number); result = mult(number); printf("multiplication = %d", result); return 0; } int mult(int n) { if (n > 0) { return n * mult(n-1); } else { return 1; } } Quiz: D is the correct option.
How does the compiler know to convert sum(number) to sum(n) since number and n are not same thing, wouldn't it be better to be consistent with sum(number) ?
'n' is used as the variable within the sum() function itself. You can think of the sum(n) part as a blueprint. When the function is called, the compiler follows the blueprint by substituting 'number' everywhere there is an 'n'.
B . condition statement is used to prevent infinite recursive calls. and #include int factorialSum(int n); int main () { int number , result ; printf("PLease Enter the positive number :"); scanf("%d", &number); result = factorialSum(number); printf(" sum = %d", result); return 0; } int factorialSum(int n){ if(n != 0){ return n + factorialSum(n-1); } else { return 1; } }
#include #include int computeFactorial(int number); int main() { int number; printf("Enter a number: "); scanf("%d", &number); printf(" The factorial of %d is %d.", number, computeFactorial(number)); return 0; } int computeFactorial(int number){ if(number == 1){ return number; } return number * computeFactorial(number - 1); }
wonderful video, programming task: #include #include int sum(int n); int main() { // Write C code here int number, result; printf("Enter a positive integer: "); scanf("%d",&number); result = sum(number); printf(" sum = %d", result); return 0; } int sum(int n){ if(n != 0 ){ //sum() function calls itself return n * sum(n-1); }else{ return n+1; } } and i think the answer to the quiz is d. none of the above?
Option D : None Of The Above --------------------------------------------------------------------------- #include int factorial(int n); int main() { int num, result; printf("Enter A Number : "); scanf("%d", &num); result = factorial(num); printf("%d! = %d",num, result); return 0; } int factorial(int n) { if (n != 0) { return n * factorial(n-1); } else { return n+1; } }
#include #include int fact(int f); int main() { int num1,result; printf("Enter the number for factorial : "); scanf("%d",&num1); result= fact(num1); printf("the factorial of %d is %d",num1,result); return 0; } int fact(int f) { if(f!=0) { return f*fact(f-1); } else return 1; }
#include int main() { int number, result; printf("Enter a number greater than 0: "); scanf("%d", &number); result = factorial(number); printf("%d", result); return 0; } int factorial(int n) { if (n != 0) { return n * factorial(n-1); } else { return 1; } }
#include int main() { int number,result; printf("Enter a positive number: "); scanf("%d",&number); result = fact(number); printf("the factorial is = %d ",result); return 0; } int fact(int n){ if (n>0) { return n * fact(n-1); } else{ return 1; } }
// Online C compiler to run C program online #include int sum (int n); int main() { int number; int result; printf("enter number: "); scanf("%d", &number); result = sum(number); printf("sum = %d", result); return 0; } int sum(int n){ if (n > 0){ return n * sum(n-1); } else{ return 1; } } number 5 output = 120
# include int a(int b); int main() { int c,d; printf("Enter any number"); scanf("%d",&c); d = a(c); printf("The result is %d", d); return 0; } int a(int b ){ if (b >0){ return b * a(b-1); } else{ return 1; } }
// Online C compiler to run C program online #include int fac(int n); int result; int main() { int number; printf("Enter a postive integer = "); scanf("%d",&number); result = fac(number); printf(" Result = %d ",result); return 0; } int fac(int n) { if (n > 0) { return n * fac(n-1); } else { return 1; } }
// To find the factorial of the given number entered by the user #include int factorial(int x ); int main(){ int n; printf("Enter a number :"); scanf("%d",&n); printf("The factorial of the given number is :%d",factorial(n)); return 0; } // Function to find factorial int factorial(int x){ if(x == 0 || x == 1){ return 1; } int fact = x * factorial(x-1); return fact ; }
#include int sum(int n); int main() { int number,result; while (1){ printf("Enter a positive Intger:"); scanf("%d",&number); result =sum(number); printf("sum:%d ",result); } return 0; } int sum(int n){ if(n !=0){ return n * sum(n-1); } else{ return 1 ; } }
#include int fact(int n); int main() { int n, f; printf("Enter a number: "); scanf("%d", &n); f= factorial(n); printf("Factorial is: %d", f); return 0; } int factorial(int a){ if (a > 0){ return a * factorial(a-1); } else { return 1; } }