This function wont work for a factorial function. Trying to figure out why.
i has been initialized to 1.Code:while(i<n) { n=n*i; i++; }
This function wont work for a factorial function. Trying to figure out why.
i has been initialized to 1.Code:while(i<n) { n=n*i; i++; }
What has n been intialized to?
If it's preset to 0, the loop will never run.
It's taken as input from the user -
Code:#include <stdio.h> /*to find factorial - considering user only enters +ve integers*/ main() { int i=1, n; printf("Enter n\n"); scanf("%d", &n); /*need to figure out the problem with the loop */ while(i<n) { n=n*i; i++; } printf("%d", n); getch(); }
Oh.. That was silly. Thank You
It works now!Code:#include <stdio.h> main() { int i=1, n, p; printf("Enter n\n"); scanf("%d", &n); p=n; while(i<n) { p=p*i; i++; } printf("%d", p); getch(); }
Additionally, read How to define main() FAQ
Also, getch() is compiler specific. Read How to get my program to wait for a keypress.