Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
C program consists of a main and a function named FindFact .In main read N integers numbers one at a time .Call FindFact by passing the read number then print the returned value from the function , the function FindFact receive number, finds and returns its factorial .
every time my code run it gives me "time limit exceeded" ! what's the problem ?

What I have tried:

Objective-C
#include <stdio.h>
int FindFact (int x);
int main(void) {
int N,i,num,y;
scanf("%d",&N);
for(i=1;i<=N;++i){
	scanf("%d",&num);
	y=FindFact(num);
	printf("factorial of %d=%d\n",y,num);
}
	return 0;
}
int FindFact(int x){
	int F=1,j;
	for(j=x;j>=1;--j){
		F*=j;
	}
	return F;
}</stdio.h>
Posted
Updated 22-Mar-16 8:13am
v3
Comments
jeron1 22-Mar-16 10:49am    
Have you stepepd through the code using a debugger?
Member 12409720 22-Mar-16 12:39pm    
it works thanks for help
jeron1 22-Mar-16 12:55pm    
Maybe update your post to describe what you did to fix the problem.
Sergey Alexandrovich Kryukov 23-Mar-16 11:02am    
Never calculate factorial using recursion. Factorial is the most popular example illustrating recursion, and is the most inappropriate. Some people even come to the totally wrong conclusion by thinking "recursion works poorly for factorial, so recursion is a bad thing". And you are doing it wrong. Non-recursive calculation of factorial is enormously more efficient...
—SA

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

You should swap y and num
C++
printf("factorial of %d=%d\n",num,y);
 
Share this answer
 
Start by simplifying it and testing the FindFact function:
C++
int main(void) 
    {
    int num, y;
    num = 6;
    y=FindFact(num);
    printf("factorial of %d=%d\n",y,num);
    return 0;
    }
If that works, then you know that it's probably not the FindFact function that causes the problem. If it doesn't, then you know it is FindFact and can focus on that.
So restore your original code, and start running it through the debugger. Look at exactly what is happening, and watch what variables contain. Try to predict what is going to happen before you execute a line of code. Does it happen? If it does, move on. If it didn't, then why not? What did happen? why didn't you expect that?
This is the first stage after coding: test and debugging - and it's a skill that like all skills needs to be used in order to improve. And it's better to learn and practice on a trivial project like this one than when faced with 20,000 lines of code! :laugh:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900