Please help me with this C program?
For the following concern, my system is being employed by 3! plus 5!.But with regard to 10 it is far from working.Can easily anyone please explain this particular, Thanks in advance
The factorial of the integer n may be the product connected with consecutive integer out of 1 in order to n.
in! = n*(n-1)**1.Write a c program this compute along with print a table involving factorial connected with any given number in.
#include
avoid main()
int some sort of, m, s=1;
printf(“Enter the extra worthiness for n:”);
scanf(“%d”, &m);
for(a=m; a>1; a–)
s=s*a;
printf(“The factorial involving %d:%d*(%d-1)*….*1=%d”, m, m, m, s);
You are having problems ’cause TWELVE! is a lot more that 32767 and you are stocked full an int.
Make use of unsigned much time instead.
do this:
#include
int main(void)
unsigned much time a, m, s=1;
printf(“Enter the extra worthiness for n:”);
scanf(“%lu”, &m);
for(a=m; a>1; a–)
s=s*a;
printf(“The factorial involving %lu:%lu*(%lu-1)*….*1=%lu”, m, m, m, s);
return 0;
when ough solve this specific 10! the right formula will take place 3628800 which is very huge value to get an int….
int can easily store not more in that case 32, 768 that will 32, 767 (maybe)…you have to use ‘double’ or perhaps ‘long int’ due to this.then your current result will certainly came accurate.long int can easily store -2, 147, 483, 648 – +2, 147, 483, 647
the following is some more information regarding data varieties:
http://www.lix.polytechnique.fr/liberti/public/computing/prog/c/C/CONCEPT/data_types.html
and
http://en.wikipedia.org/wiki/Integer_%28computer_science%29
include
avoid main()
int some sort of, m;
much time int s=1;
for(m=0; m<20; m++)
for(s=1, a=m; a>1; a–)
s=s*a;
printf(“\n %d! = %ld”, m, s);
getch();
nothing wrong with all the logic…
however u forgot tat….int (2byte) can easily hold ideals only upto 65535…
however 9! is 362880 which crosses the stove…
in case we work with long, (4 byte) you can easliy hold upto, 2(4*8) roughly
Without verifying the ideals, it appears the price 10! is too major to save in ersus.Change that declaration for s to somewhat of a different form eg long.
Leave a Reply
You must be logged in to post a comment.