| | |
need help with factorial using recursion.
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2005
Posts: 82
Reputation:
Solved Threads: 1
hi i am having a problem with this code.for every input i am getting the same answer 32.any help would be highly appreciated
C Syntax (Toggle Plain Text)
/*program to find the factorial of a given number*/ #include<stdio.h> main() { int n,fact; int facto(int ); printf("\nenter the number for which you want to find the factorial"); scanf("%d",&n); fact=facto(n); printf("\nthe factorial of yhe number %d is %d",n,fact); } int facto(int n) { int k; if(n==1) return(1); else k=n*fact(n-1); return(k); }
This won't compile:
k=n*fact(n-1);
should be
k=n*facto(n-1);
After making that change, seems to work fine..
enter the number for which you want to find the factorial
7
the factorial of yhe number 7 is 5040
(Be careful though factorials get large very quickly and you have a risk of blowing out the size of an integer, etc.)
k=n*fact(n-1);
should be
k=n*facto(n-1);
After making that change, seems to work fine..
enter the number for which you want to find the factorial
7
the factorial of yhe number 7 is 5040
(Be careful though factorials get large very quickly and you have a risk of blowing out the size of an integer, etc.)
Also, this should be
C Syntax (Toggle Plain Text)
if (n == 0) return 1;
/*program to find the factorial of a given number*/
#include<stdio.h>
main()
{
int fact(int );
/*-----------------------------------------------------*/
int n,fact;
printf("\nenter the number for which you want to find the factorial");
scanf("%d",&n);
fact=fact(n);
printf("\nthe factorial of yhe number %d is %d",n,fact);
}
/*-----------------------------------------------------*/
int fact(int n)
{
if(n==1) return(1);
else return n*fact(n-1);
}•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> int factorial (int); void main() { auto int num , res; clrscr(); printf("Enter the Number....."); scanf("%d",&num); res = factorial(num); printf("Factorial Of %d Is %d" , num , res); getch(); } int factorial(int num) { int res =1; if(num!=0) res = num * factorial(num-1); return res; }
Last edited by ##k.k##; Aug 1st, 2009 at 4:03 am.
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> int factorial (int); void main() { auto int num , res; clrscr(); printf("Enter the Number....."); scanf("%d",&num); res = factorial(num); printf("Factorial Of %d Is %d" , num , res); getch(); } int factorial(int num) { int res =1; if(num!=0) res = num * factorial(num-1); return res; }
Last edited by ##k.k##; Aug 1st, 2009 at 4:03 am.
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
Sorry Bro......I didn't see that the post is 4 year ago.........
I am just searching .......for this code.....i mean correct one...
but i didn't found the correct one.......
So i write it down......
I m bit confuse in 0!=1 or ZERO.....thats why....i am in search of this code....
By the way .....what is wrong with void main?????
I am just searching .......for this code.....i mean correct one...
but i didn't found the correct one.......
So i write it down......
I m bit confuse in 0!=1 or ZERO.....thats why....i am in search of this code....
By the way .....what is wrong with void main?????
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
/****************C Program for factorial by using recursion**************/
#include<stdio.h>
int fact(int a);
int facto(int s) //this function will calculate by recursive
{
if((s==0)||(s==1))
return 1;
else
return(s*facto(s-1));
}
main()
{ int n,result,result1;
printf("Pls enter ur num\n");
scanf("%d",&n);
result=facto(n);
result1=fact(n);
printf("facto of ur num= %d\n fact of ur num =%d\n",result,result1);
}
/***************************function for factorial without recursive*************/
int fact(int a)
{ int i,c=1;
for(i=1;i<=a;i++)
c=c*i;
return(c);
}
#include<stdio.h>
int fact(int a);
int facto(int s) //this function will calculate by recursive
{
if((s==0)||(s==1))
return 1;
else
return(s*facto(s-1));
}
main()
{ int n,result,result1;
printf("Pls enter ur num\n");
scanf("%d",&n);
result=facto(n);
result1=fact(n);
printf("facto of ur num= %d\n fact of ur num =%d\n",result,result1);
}
/***************************function for factorial without recursive*************/
int fact(int a)
{ int i,c=1;
for(i=1;i<=a;i++)
c=c*i;
return(c);
}
![]() |
Similar Threads
- Order of a recursive function (C)
- factorial using a for loop (C++)
- Permutaions With Recursion (C++)
- need Recursion help (C)
- Recursion (C++)
- Factorial? (C++)
Other Threads in the C Forum
- Previous Thread: How to do it?
- Next Thread: Int-datum Reader Program
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






