answersLogoWhite

0

#include<stdio.h>

main()

{

int a;

printf("Enter a");

scanf("%d",&a);

a=fun(a);

printf("%d",a);

}

int fun(int x)

{

if(x==1)

{

return 1;

}

else

{

x=x*fun(x-1);

}

return x;

}

User Avatar

Wiki User

13y ago

Still curious? Ask our experts.

Chat with our AI personalities

ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve
CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
More answers

unsigned long nfact (unsigned long N) return N==1 ? 1 : nfact (N-1);

This algorithm, like all limited precision algorithms, suffers from overflow after only a few recursions. If, for instance, an unsigned long was 32 bits long, the largest factorial that could be computed would by 12! or 479,001,600. 13! or 6,227,020,800 would overflow the maximum 32 bit unsigned result of 4,294,967,295.

Attached is an arbitrary length algorithm, based on linked lists of decimals, that will compute factorials to nearly any length. It also calculates them using less sophisticated means, with varying precisions, so you can see where the overflow occurs. In order to compute very very large factorials, you will need to tell your linker to increase the run-time stack size.

// Factorial.cpp : Defines the entry point for the console application.

//

#include <stdlib.h>

#include <stdio.h>

/* Microsoft 32-bit iterative */

unsigned long NFactLongIterative (unsigned long N) {

unsigned long result = N;

if (N < 2) return 1;

if (N 2) {

decimal_initialize (d, 2);

return;

}

while (N > 2) {

decimal_multiply (d, N);

N--;

}

return;

}

/* Example main line */

/* Generates all variations to show differences in results */

int main (int argc, char *argv[]) {

int N;

decimal Decimal = {2, NULL};

if (argc < 2) {

printf ("Enter N (or use command line) : ");

scanf_s ("%d", &N);

} else {

N = atoi (argv[1]);

}

printf ("Long: %u! = %u\n", N, NFactLongIterative (N));

printf ("LongLong: %u! = %I64u\n", N, NFactLongLongIterative (N));

printf ("Recursive: %u! = %I64u\n", N, NFactLongLongRecursive (N));

printf ("Double: %u! = %.0f\n", N, NFactDouble (N));

/* note: arbitrary is exact - if the others don't match, arithmetic overflow occurred */

printf ("Arbitrary: %u! = ", N);

decimal_NFactIterative (&Decimal, N);

decimal_print_digits (&Decimal, true);

return 0;

}

User Avatar

Wiki User

14y ago
User Avatar

#include<stdio.h>

main()

{

int i,num,fact=1;

printf("enter the number");

scanf("%d",&num);

for(num=num;num > 0;num--)

{

printf("%d",num);

fact=fact*num;

}

printf("the factorial is %d",fact);

}

User Avatar

Wiki User

12y ago
User Avatar

int factorial(int n)

{

int total = 1;

for(; n > 1; n--)

total *= n;

return total;

}

User Avatar

Wiki User

15y ago
User Avatar

c program maze

User Avatar

Wiki User

14y ago
User Avatar

Add your answer:

Earn +20 pts
Q: Factorial of a number using recursion?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

2 Write a program in java to find factorial of a number?

import java.math.BigInteger; public class Factorial { public static void main(String[] args) { BigInteger n = BigInteger.ONE; for (int i=1; i&lt;=20; i++) { n = n.multiply(BigInteger.valueOf(i)); System.out.println(i + "! = " + n); }


What is the algorithm n flowchart for calculating factorial of number using recursion?

A flowchart for factorial of number can be made using different software's. Microsoft Word is the most popular software for beginners to make simple flowcharts.In order to draw the flowchart write the number at the top of the paper and then draw two lines under it going slightly to the sides. Decide what two numbers can be multiplied to equal that number. Keep going until you can no longer get to smaller numbers.


Algorithm to find factorial using functions?

factorial using recursion style in c++ is unsigned int fact(unsigned int a) { if (a&lt;=1) return 1; else { f*=fact(a-1); return a; } } when using looping structure factorial is unsigned int fact (unsigned int n) { unsigned int i,f=1; for(i=1;i&lt;=n;i++) f*=i ; return f; }


Program to find factorial of a number using inheritance?

/*71.PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION*/ #include&lt;stdio.h&gt; #include&lt;conio.h&gt; int fact(int); void main() { int n,f; clrscr(); printf("Enter number whose factorial is to be calculated: "); scanf("%d",&amp;n); if(n&gt;0) { f=fact(n); printf("factorial of %d is %d",n,f); } else printf("Factorial of numbers less than 1 does not exist"); getch(); } int fact(int n) { int facto=1; if(n&gt;1) facto=n*fact(n-1); else return 1; return(facto); }


Write a recursive procedure to compute the factorial of a number?

#include &lt;iostream&gt; using namespace std; int main() { int i, number=0, factorial=1; // User input must be an integer number between 1 and 10 while(number&lt;1 number&gt;10) { cout &lt;&lt; "Enter integer number (1-10) = "; cin &gt;&gt; number; } // Calculate the factorial with a FOR loop for(i=1; i&lt;=number; i++) { factorial = factorial*i; } // Output result cout &lt;&lt; "Factorial = " &lt;&lt; factorial &lt;&lt; endl;