Quantcast
answersLogoWhite
notificationBell

Top Answer
User Avatar
Wiki User
Answered 2010-01-15 19:15:04

// Iterative solution

unsigned long iterativeFactorial(const unsigned long n) {

unsigned long i, factorial = 1;

for(i = 1; i <= n; i++) {

factorial *= i;

}

return factorial;

}

// Recursive solution

unsigned long recursiveFactorial(const unsigned long n) {

if(n <= 1) {

return n;

}

return n * recursiveFactorial(n - 1);

}

// Sample calls

int main() {

unsigned long n;

printf("Enter a number to find its factorial: ");

scanf("%u",&n);

printf("Iterative factorial: %u\n", iterativeFactorial(n));

printf("Recursive factorial: %u\n", recursiveFactorial(n));

return 0;

}

001
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0
User Avatar

Your Answer

Still Have Questions?

Related Questions

Write a program to calculate the factorial of a given number?

/*this is the program to find the factorial to n numbers*/ #include&lt;stdio.h&gt; void main() { int n,f,factorial(int); printf("which number facrorial you want?"); scanf("%d",&amp;n); f=factorial(n); printf("The factorial upto %d is %d",n,f); } int factorial(int a) { if(a==0) return 1; else return (a)*factorial(a-1); }


How do you write a program that calculate factorial in javascript?

function factorial(n) { var x=1; while(n&gt;1) x*=(n--); return x; }


How do you write the program for factorial?

unsigned factorial (unsigned n) { return n


How do you write flowchart to find factorial of given number using recursive and non recursive?

1. flochart of cp Program to find factorial of number using function


Write a program to find the factorial value of the given number using for loop?

In Java (not tested): long factorial(int number) { long result = 1; for (int i = 1; i


Using do while statement write cprogram for factorial number?

using c program write factorial number with do..while statement


Write a C-like program fragment that calculate the factorial function for argment 12 with do while loop?

#!/usr/bin/perl print factorial($ARGV[11]); sub factorial { my($num) = @_; if($num == 1) { return 1; # stop at 1, factorial doesn't multiply times zero } else { return $num * factorial($num - 1); # call factorial function recursively } }


How do you write a code that calculates the factorial of a number using while loop?

Program to calculate factorial using while loop in c#include#includevoid main(){int i,fact=1,a;printf("enter the num whose factorial u want to take out");scanf("%d",&a);i=a;while(i>0){fact=fact*i;i--;}printf("factorial=%d",fact);getch();}Program to calculate factorial using while loop in c++#include#includevoid main(){int i,fact=1,a;coutcin>>a;i=a;while(i>0){fact=fact*i;i--;}coutgetch();}-Shruti Jain


How do you write a program that calculate factorial?

#include&lt;conio.h&gt; #include&lt;stdio.h&gt; void main() { clrscr(); int i=1,fact=1,n; printf("enter a no."); scanf("%d"n); while(i&lt;=n) { fact=i*fact; i++; } printf("factorial=%d",fact); getch(); }


Write a program in 8086 assembly language to generate the factorial of given number?

TITLE Calculating a Factorial (Fact.asm) ; This program uses recursion to calculate the ; factorial of an integer. ; Last update: 06/01/2006 INCLUDE Irvine32.inc .code main PROC push 12 ; calculate 12 factorial call Factorial ; calculate factorial (eax) ReturnMain: call WriteDec ; display it call Crlf exit main ENDP Factorial PROC push ebp mov ebp,esp mov eax,[ebp+8] ; get n cmp eax,0 ; n &lt; 0? ja L1 ; yes: continue mov eax,1 ; no: return 1 jmp L2 L1: dec eax push eax ; Factorial(n-1) call Factorial ; Instructions from this point on execute when each ; recursive call returns. ReturnFact: mov ebx,[ebp+8] ; get n mul ebx ; ax = ax * bx L2: pop ebp ; return EAX ret 4 ; clean up stack Factorial ENDP END main


What is the benefit of using function.llustrate different ways of passing argument to function in c plus plus?

Functions are used to reduce the lines of code and the complexity of the code. For an instance let us suppose that you want to calculate the factorial of numbers at different times in a program. There are two ways to do this 1. Write a 4-5 line code every time you want to calculate factorial. 2. Write a function of 4-5 lines which calculates the factorial and call that function every time you need to calculate factorial by just writing a single line. In C++ you can pass the variable, address of the variable or a reference to the variable in a function


1.Write a c program for Fibonacci series?

/*program to calculate factorial of a number*/ #include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { long int n; int a=1; clrscr(); printf("enter the number="); scanf("%ld",&amp;n); while(n&gt;0) { a*=n; n--; } printf("the factorial is %ld",a); getch(); }


Write a function to calculate the factorial value of any integer entered through the keyboard?

#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;


Write a C program to calculate a factorial?

#include#includevoid main(){int a,b,c=1;clrscr();{printf("enter the number b=");scanf("%d",&b);}for(a=b;a>=1;a--)c=c*a;{printf("the factorial of the num is%d",c);}getch();}


How to write a C program to find factorial of given integer using recursive function?

// Note: You may need a larger data type, factorials become very big very quickly and may cause an overflow long factorial(int x) { if(x == 1) return 1; return((long) factorial(x-1) * x); }


7 Write a C program to compute the factorial of a number using for loop?

int factorial(int n) { int i; int f=1; for(i=2;i&lt;=n;++i) f*=i; return f; }



Write a C program to extract a given word from a file?

program to extract a given word from a file


A Write a program that reads a nonnegative integer and computes and prints its factorial?

int main() { // Variable declarations. unsigned long int factorial = 1 , number = 1; // reads a number for finding its factorial. cout > number; while( number > 1 ) { factorial *= number * ( number - 1 ); number -= 2; } cout return 0;


Write a c program to find simple interest?

write a program in c to calculate simple interest for 5 years


Write a program that the user will enter a number and display the factorial number and it's answer?

#include &lt;iostream&gt; #include &lt;cctype&gt; using std::cin; using std::cout; using std::endl; using std::tolower; long factorial(const int&amp; N); int main() { int N = 0; //factorial of N char command = 'n'; do { cout &lt;&lt; "Enter a number to calculate factorial: "; cin &gt;&gt; N; cout &lt;&lt; endl &lt;&lt; "Factorial of " &lt;&lt; N &lt;&lt; " is: " &lt;&lt; factorial(N) &lt;&lt; endl; cout &lt;&lt; "Do you want to continue (y/n)?"; cin &gt;&gt; command; cout &lt;&lt; endl; } while ('y' 0) { return 1; } else { return (N * factorial(N - 1)); } }



Write an algorithm to print the factorial of given number and then draw the flowchart?

write an algorithm to print the factorial of a given number and then draw the flowchart. This looks like someones homework, defiantly someone looking for the easy way. When it comes to programming, the more you do the better you get. Experience counts (making your own mistakes and learning from the mistake).


Write a program in c language to sum and average a given number?

The sum and average of a given number is just that number, so there is no need to write a program.


Write a java script program to print first ten odd natural numbers in C?

Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.


Still have questions?

Trending Questions
How to Make Money Online? Asked By Wiki User
Best foods for weight loss? Asked By Wiki User
Does Neil Robertson wear a wig? Asked By Wiki User
Unanswered Questions