> Here's my factorial program:
> int main()
Should be int main(void), according to the C standards. What
you've written will work with most compilers, but strictly
speaking it's incorrect.
> {
> int n;
> scanf("%d", &n);
OK, but you should prompt the user with something like
"Enter a positive integer:" and check to see whether the
user supplies that. Always check your inputs, even if you
*know* they must be right: GIGO. What happens if someone
enters -3, or 1234567890?
> int j = 1;
> int i;
>
> for(i = 1; i <= n; i++)
> {
> j = i*j;
> }
OK, but a bit long-winded. How about this:
int j = 1;
for ( ; n > 1; n--)
j *= n;
Or maybe:
int j = 1;
while (n-- > 1)
j *= n;
This idea of taking an input variable and decrementing it
towards zero is quite common in C. BTW, you don't need to
multiply j by 1, it's a waste of time!
I'm glad to see you've considered the corner case of 0! =>
1. :-)
> printf("%d", j);
> }
Main is supposed to return an int. The conventional value is
zero, signifying nothing has gone wrong.
return 0;
> I'm not quite sure how to
> write pow function, I will have to look into that.
Try something along similar lines to the factorial function.
David