68.3k views
4 votes
Debug and correct the code below to calculate factorial correctly. Write a report on the flow of the code, addressing each variable as you debug the code. Include screenshot of your debug watches.

Note : factorial calculation formula : n! = n x (n-1)!
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - -
#include
Int.main()
{
Int i, num, j:
Printf("Enter the number:");
Scanf("%",&num);
For (i=1;I,num’i++)
J=j*i;
Printf("The factorialof %d is %d\\",num,j);
}

User Pieter VDE
by
8.1k points

1 Answer

1 vote

Final answer:

To calculate the factorial correctly, there are a few errors in the provided code. The corrected code is included.

Step-by-step explanation:

To calculate the factorial correctly, there are a few errors in the provided code that need to be fixed:

  • The line 'Printf("Enter the number:");' is missing a semicolon at the end.
  • The line 'Scanf("%",&num);' should have '%d' instead of '%'. Additionally, there should be an ampersand (&) before 'num' to properly read the input value.
  • The 'For' loop condition should be 'i<=num' instead of 'i,num'.
  • The line 'J=j*i;' should be 'j=j*i;' to properly assign the calculated value to 'j'.

Here is the corrected code:

#include <stdio.h>

int main() {
int i, num, j;
printf("Enter the number: ");
scanf("%d",&num);
j=1;
for (i=1; i<=num; i++) {
j=j*i;
}
printf("The factorial of %d is %d
",num,j);
return 0;
}

User Mohit Mutha
by
7.5k points