// 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;
}
/*this is the program to find the factorial to n numbers*/ #include<stdio.h> void main() { int n,f,factorial(int); printf("which number facrorial you want?"); scanf("%d",&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); }
function factorial(n) { var x=1; while(n>1) x*=(n--); return x; }
unsigned factorial (unsigned n) { return n
1. flochart of cp Program to find factorial of number using function
In Java (not tested): long factorial(int number) { long result = 1; for (int i = 1; i
using c program write factorial number with do..while statement
#!/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 } }
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
#include<conio.h> #include<stdio.h> void main() { clrscr(); int i=1,fact=1,n; printf("enter a no."); scanf("%d"n); while(i<=n) { fact=i*fact; i++; } printf("factorial=%d",fact); getch(); }
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 < 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
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
/*program to calculate factorial of a number*/ #include<stdio.h> #include<conio.h> void main() { long int n; int a=1; clrscr(); printf("enter the number="); scanf("%ld",&n); while(n>0) { a*=n; n--; } printf("the factorial is %ld",a); getch(); }
#include <iostream> using namespace std; int main() { int i, number=0, factorial=1; // User input must be an integer number between 1 and 10 while(number<1 number>10) { cout << "Enter integer number (1-10) = "; cin >> number; } // Calculate the factorial with a FOR loop for(i=1; i<=number; i++) { factorial = factorial*i; } // Output result cout << "Factorial = " << factorial << endl;
#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();}
// 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); }
int factorial(int n) { int i; int f=1; for(i=2;i<=n;++i) f*=i; return f; }
calculate pay of an employee.
program to extract a given word from a file
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 program in c to calculate simple interest for 5 years
#include <iostream> #include <cctype> using std::cin; using std::cout; using std::endl; using std::tolower; long factorial(const int& N); int main() { int N = 0; //factorial of N char command = 'n'; do { cout << "Enter a number to calculate factorial: "; cin >> N; cout << endl << "Factorial of " << N << " is: " << factorial(N) << endl; cout << "Do you want to continue (y/n)?"; cin >> command; cout << endl; } while ('y' 0) { return 1; } else { return (N * factorial(N - 1)); } }
Solve simple intress usin fortran
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).
The sum and average of a given number is just that number, so there is no need to write a program.
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.