Why does my FirstFactorial program keep looping back to while condition even after the condition is not met

while loop
do-while loop
do while loop in c
for loop in r
while loop c++
do while loop c++
r while loop multiple conditions
for loop in c

Here's the code snippet, this when run with number 4 outputs 2424242448484848288288288288576576576576. Not sure as to why would the execution would jump back to while loop after exiting the function code. Any help will be appreciated. Thank you in advance.

#include <stdio.h>
#include <string.h>

int result = 1;
void FirstFactorial(int);

void FirstFactorial(int num) {
    // code goes here
    while (num > 0) {
        result = result * num;
        num--;
        FirstFactorial(num);
    }
    printf("%d", result);
}

int main(void) {
    int var;
    // keep this function call here
    printf ("Enter your no.\n");
    scanf("%d", &var);
    FirstFactorial(var);
    return 0;
}

Within the function

void
FirstFactorial(int num)
{
  // code goes here
  while(num > 0)
    {
      result = result * num;
      num--;
      FirstFactorial(num);
    }
  printf("%d", result);
}

each its iteration calls itself num times and all iterations together output the global variable result.

So for example in the first call of the function the function calls itself in the while loop for the range of values [num, 1].

Remove the while loop and do not use the global variable.

Here is a demonstrative program.

#include <stdio.h>

unsigned long long int factorial( unsigned long long int n )
{
    return n < 2 ? 1 : n * factorial( n - 1 );
}

int main(void) 
{
    printf( "%llu! = %llu\n", 4llu, factorial( 4 ) );
    printf( "%llu! = %llu\n", 20llu, factorial( 20 ) );

    return 0;
}

The program output is

4! = 24
20! = 2432902008176640000

Pay attention that the maximum value you may specify is 20.

Loops: while and for, While the condition is truthy, the code from the loop body is executed. ways to stop such loops, and in server-side JavaScript, we can kill the Any expression or variable can be a loop condition, not just comparisons: the condition is This form of syntax should only be used when you want the body of� The do while loop differs significantly from the while loop because in do while loop statements in the body are executed at least once even if the condition is false. In the case of while loop the condition is checked first and if it true only then the statements in the body of the loop are executed. The following program print numbers between

Either you implement the factorial with a loop or you do it recursively.

Both ways are feasible but your code mixes it up.

The do while loop in C, do{ // body of do while loop statement 1; statement 2; }while(condition); do while loop statements in the body are executed at least once even if the The following program print numbers between 1 and 100 which are i by 1 }while(i < 100); // stop the loop when i becomes greater than 100 Back to top� This process keeps repeating until the condition becomes false. Therefore, you must always include a statement which alters the value of the condition so that it ultimately becomes false at some point. Each execution of the loop body is known as iteration. The following program uses while loop to prints all even numbers between 1 to 100: Example 1:

Your function mixes iterative and recursive approaches. You can correct it by removing the useless recursion which causes multiple intermediary results to be computed and printed. Defining result as a global variable is also a mistake, especially since you do not reinitialize it before the loop. Using type long long will allow for larger factorials to be computed. Adding a trailing \n after the printf conversion specifier is advisable too.

Here is a corrected version:

#include <stdio.h>

void FirstFactorial(int num) {
    long long result = 1;

    while (num > 1) {
        result = result * num;
        num--;
    }
    printf("%lld\n", result);
}

int main(void) {
    int var;
    // keep this function call here
    printf("Enter your number\n");
    if (scanf("%d", &var) == 1)
        FirstFactorial(var);
    return 0;
}

The while loop in C, Loops are used to execute statements or block of statements repeatedly. Just like the if-else statement, the while loop starts with a condition. This process keeps repeating until the condition becomes false. The following program uses while loop to prints all even numbers between 1 to Back to top� Read the file back in until there are no more lines. My reason for showing this example is to illustrate the fact that sometimes looping until/while a condition is true is the only way to go! Let's start with what the output of the program will look like:

R while Loop (With Examples), In this article, you will learn to create a while loop in R programming. In R programming, while loops are used to loop until a specific condition is met. The statements inside the loop are executed and the flow returns to evaluate the will not be able to monitor its performance. Cookies Details. Confirm My Choices. Back� If it contains only one statement, then the curly braces are not compulsory. It is a good practice though to use the curly braces even we have a single statement in the body. In while loop, if the condition is not true, then the body of a loop will not be executed, not even once. It is different in do while loop which we will see shortly

Python "while" Loops (Indefinite Iteration) – Real Python, The while Loop; The Python break and continue Statements; The else Clause the designated block is executed repeatedly as long as some condition is met. When the body of the loop has finished, program execution returns to the top of the First of all, lists are usually processed with definite iteration, not a while loop. The syntax of the while loop is: while (condition) { // body of the loop } Here, A while loop evaluates the condition; If the condition evaluates to true, the code inside the while loop is executed. The condition is evaluated again. This process continues until the condition is false. When the condition evaluates to false, the loop terminates.

while loop to repeat when condition is true, while. while loop to repeat when condition is true. collapse all in page Count the number of lines of code in the file magic.m . If you inadvertently create an infinite loop (that is, a loop that never ends on its own), stop execution of the loop by pressing Ctrl+C. do % Not valid MATLAB syntax statements while expression. while expression, statements, end evaluates an expression, and repeats the execution of a group of statements in a loop while the expression is true.An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric).

Comments
  • Because it is a different (local) num in the called function. The caller's num has still only decremented once when the recursive call returns. And, as the recursion drops back down, even function exit prints a value (with no separator).
  • Note, an int's maximum value is most likely 2,147,483,647. This allows a factorial of, at most, 12!.
  • @WeatherVane When you say different num, I don't understand. Can you please explain?
  • @FiddlingBits Yes, I am just trying out a 4! but still prints out some weird things
  • I don't know if you have grasped recursion, but suppose each time it calls itself it is given a suffix, like FirstFactorial_1(int num_1) and then FirstFactorial_2(int num_2) etc. and each is independent of the previous one. When each one ends, it passes control back to the previous one - it doesn't just stop. And each one prints a message on the way out, and these messages are all run into each other because there is no newline.
  • never hire a developer who computes the factorial using recursion :-)