I have been trying to calculate factorials of numbers from 0 to 10 using for loop and pass by reference. when I executes the lines outside of the loop(those which are now commented out) it works fine but when I use the loop parameter's pointer as the input, it out puts the following regardless of the range of i: 1 1 2 6 5040 & Here is my code:
#include<stdio.h>
int fact(int*);
int main(void)
{
// int n;
// scanf("%d",&n);
// printf("%d\n",fact(&n));
for (int i = 0; i < 15; i++)
{
printf("%d\n",fact(&i));
}
return 0;
}
int fact(int *a)
{
if (*a==0 || *a==1)
{
return (1) ;
}
for (int i = *a -1; i > 0; i--)
{
*a*=i;
}
return *a;
}
& when I try to debug, the panel just prints that the build was finished successfully and when I press Enter it closes. I have tried all YouTube guides with no success.