C Newbie Problem with Factorial Example
Can anyone please help me with my logic here , i can't seem to figure it out, however when i try to find Factorial of num 2 i get the factorial as 2 which is right , but other numbers give me huge negative numbers , WHY ??
please point out my mistake so i can learn , any help appreciated
Thank you
Have a nice day!
I'm trying to learn C on my own , i won't give up this time so many times i've tried and gave up half-way especially when i start pointers, will you guys help me !!!
Code:
#include<stdio.h>
int fact();
int main() {
int n,res;
printf("Enter a Factorial :\n");
scanf("%d",&n);
res = fact(n);
printf("The Factorials of %d = : %d \n",n,res);
return 0;
}
int fact(int num)
{
int i=1;
if(num == 0) {
num =1;
}else{
while(num >i) {
num = num *i;
i++;
}
}
return num;
}