Hey guys. Need some Loop Help.

Jan 4, 2014 at 10:32am
Hey Guys. I needed a loop that asks the user if he/she would like to compute. [turbo C]
Thank you and Happy new year~ :)


Here is my code:




#include <stdio.h>
#include <stdlib.h>
int num, fact = 1;
main()
{

printf("Input a non-negative integer: \n ");
scanf("%d", &num);

for(int i=1; i<=num; i++){
fact = fact * i;
}

printf("The factorial of %d is %d", num, fact);

system ("pause");
}


Please use turbo C. :)
Last edited on Jan 4, 2014 at 11:02pm
Jan 4, 2014 at 11:35am
In that case, you should create your own factorial function. If you remember the definition of a factorial, it should't be that difficult. Also, it's common to use integers for iterating a for loop. And finally, creating a factorial like that will not support decimals, and therefore, there is no reason to use a float. Your entire code can be rewritten to use integers!

EDIT: By the way... is that main function definition allowed C syntax? I am not very experienced with C, but I highly doubt that is acceptable code.
Last edited on Jan 4, 2014 at 11:37am
Jan 4, 2014 at 1:48pm
before creating any program you have to check it on daily routine and its all steps .. then change same thing in coding .. that will be easy for you .. specily for loops...
Topic archived. No new replies allowed.