After that, again, the condition will be checked. If the condition is false, it will jump to the code after the For loop without executing the For loop code.Īfter the For loop, the increment statement will be executed. If the condition is true, then it will execute the code inside the block of For loop. In this step, you can initialize and declare variables for the code. In the For loop, the first initialization step is executed and only once in the whole program. The output for the positive number is as follows. If the condition first checks if the given number is negative or not, if it is negative, it will execute if the statement and throw the error and stop the program.Īnd if the given number is positive, it will transfer control to else statement and condition are given in the else statement is executed, and it will calculate the factorial for a given number. Then scan function is used to allow a user to enter the number by their wish. In the above example, we have initialized three variables number, i.e. Printf("Factorial of the given number %d is %llu", number, fact) Printf("Error! You have entered negative number and Factorial for negative number does not exist.") Printf("Enter the positive number to find the factorial: ") Let us see how we can calculate factorial using the if-else statement. If the statement in if the condition is not true, it transfers the control to the else statement and else statement is being executed. If the statement is evaluated in an if-else statement, if the statement in it is true, it will give the output. In this section, we are going to discuss how factorial is calculated in the C program using different methods. N! = n ( n – 1)( n – 2)( n – 3) …… Examples of Factorial in C by Using various method
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |