The output of the code is ___. int fact=1; for (i=1; 4; i++) { fact=fact*i; printf(“%d”, fact); } ans. Infinite loop 6 120 24
Question
The output of the code is ___.
int fact=1;
for (i=1; 4; i++)
{
fact=fact*i;
printf("%d", fact);
}
ans.
- Infinite loop
- 6
- 120
- 24
Solution
The code provided is incorrect and will result in an error. The condition in the for loop is not properly defined. It should be something like "i <= 4" if you want to calculate the factorial of 4.
Here is the corrected code:
#include <stdio.h>
int main() {
int fact = 1;
for (int i = 1; i <= 4; i++) {
fact = fact * i;
printf("%d\n", fact);
}
return 0;
}
This code will output:
1 2 6 24
This is because it calculates the factorial of 4 step by step. The factorial of a number n is the product of all positive integers less than or equal to n. So for 4, it's 123*4 = 24. The code prints the intermediate results as well.
Similar Questions
The output of the code is ___.int fact=1;for (i=1; 4; i++){ fact=fact*i; printf(“%d”, fact);}ans.12024Infinite loop6 Previous Marked for Review Save & Finish
The output of the code will be ____. int i=5; printf(“%d %d %d”, i, i++, ++i); ans. 7 6 5 7 6 6 5 5 7 7 6 7
main( ){int i, j ;for ( i = 1 ; i <= 2 ; i++ ){for ( j = 1 ; j <= 2 ; j++ ){if ( i == j )continue ;printf ( “n%d %dn”, i, j ) ;}}}1 22 12 11 23 22 3Error
What is the output of the following piece of code?int i;i = 10;while (i < 20){ printf("%d", i % 2); i++;}
What will be the output of the following Pseudocode?int c = 0, d = 0 while ( d< 6 ) d=d+2 c=c+1 end while loop print c3564
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.