/*program for finding the factorial*/
void main()
{
int a,b=1;
clrscr();
printf("Please enter any number to find its factorial\n");
scanf("%d",&a);
while(a>1)
{
b=b*a;
a--;
}
printf("factorial is %d",b);
getch();
}
//Factorial using recursive function
#include <stdio.h>
//To increase the limit, the return type should be changed to unsigned long
int fact(int number)
{
if (number == 0)
return 1;
return fact(number-1) * number;
}
int main(void)
{
int number ;
printf("Enter the number to find the factorial:");
scanf("%d",&number);
printf("The factorial is:%d",fact(number));
return 0;
}
#include <cstdio>
int fact(int x);
int main()
{
int x, total;
printf("Enter a number: ");
scanf("%d", &x);
total = fact(x);
printf("The factorial of %d is %d", x, total);
char wait;
scanf("%s", wait);
return 0;
}
int fact(int x)
{
if(x == 1)
{
return 1;
}
return fact(x - 1) * x;
}
#include <iostream>
unsigned int factorial(const unsigned int n)
{
// The factorial of 0 and 1 (0! and 1!) is 1.
if(n<2)
return(1);
// All other values are the product of n and n-1.
return(factorial(n-1)*n);
}
int main()
{
std::cout<<"The factorial of 5 is "<<factorial(5)<<std::endl;
}
Note that a 32-bit unsigned int can accomodate factorials up to 12! while 64-bits would allow up to 20!. Floating point types with 2-digit exponents can accomodate up to 69!. Even the scientific calculator in Windows 7 and 8 can only handle up to 3248!. To accomodate larger values you must implement a user-defined dynamic integer that uses as many bits as necessary.
This program uses a non recursive portable factorial algorithm that supports arbitrary length decimal arithmetic in a linked list. The primary algorithm is decimal_NFactIterative. For vary large values, you may need to increase your binder stack size.
#include <stdlib.h>
#include <stdio.h>
/* Portable arbitrary length decimal iterative */
/* one node of a linked list of digits, the first node being low-order */
struct _decimal {
int digit;
struct _decimal *next;
};
typedef struct _decimal decimal;
/* Portable arbitrary length decimal iterative */
/* Initialize the list - necessary on second pass, if main recoded */
void decimal_initialize (decimal *d, int n) {
decimal *next, *nextsave;
d->digit = n;
nextsave = d->next;
d->next = NULL;
next = nextsave;
while (next != NULL) {
nextsave = next->next;
free (next);
next = nextsave;
}
return;
}
/* Portable arbitrary length decimal iterative */
/* Append a digit at the high order position */
void decimal_add_digit (decimal *d, int n) {
decimal *new_digit = (decimal*) malloc (sizeof (decimal));
while (d->next != NULL) d = d->next;
new_digit->digit = n;
new_digit->next = NULL;
d->next = new_digit;
return;
}
/* Portable arbitrary length decimal iterative */
/* Print the digits in reverse order - recursive */
void decimal_print_digits (decimal *d, int last_digit) {
if (d->next != NULL) decimal_print_digits (d->next, false);
printf ("%d", d->digit);
if (last_digit) printf("\n");
return;
}
/* Portable arbitrary length decimal iterative */
/* multiply the list by N */
void decimal_multiply (decimal *d, int N) {
int carry = 0;
while (d != NULL) {
d->digit = d->digit * N + carry;
carry = d->digit / 10;
d->digit %= 10;
if (carry != 0 && d->next 2) {
decimal_initialize (d, 2);
return;
}
while (N > 2) {
decimal_multiply (d, N);
N--;
}
return;
}
/* Example main line */
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 ("Arbitrary: %u! = ", N);
decimal_NFactIterative (&Decimal, N);
decimal_print_digits (&Decimal, true);
return 0;
}
factorial is simply multiplication of n-1 to 1
there are majorly two approaches
both have advantages on each other.
both codes and algorithm is shred on theprogrammingx.co.uk just visit and confirm.
Let's see the factorial Program using loop.
#include
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i
#include
int main()
l
hope you like the answers
Pseudo code+factorial
by this program you can find the factorial: #include<iostream> using namespace std; main() { int n,x,f=1; cin>> n; x=0; while(x<n) { x++; f= f*x; } cout<<"factorial is"<<f<<"\n"; system("pause"); return 0; }
== == using recursions: unsigned int Factorial( unsigned int x) { if(x>0) { return ( x * Factorial(x-1)); } else { return(1); } } factorial: unsigned int Factorial( unsigned int x) { unsigned int u32fact = 1; if( x == 0) { return(1); } else { while(x>0) { u32fact = u32fact *x; x--; } } }
// 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; }
The factorial f(n) = n * (n-1) * (n-2) * .. 1. For example factorial 5 (written as 5!) = 5 x 4 x 3 x 2 x 1 = 120. The function below returns the factorial of the parameter n. int factorial( int n) { if (n==1) return 1 else return n* factorial( n-1) ; }
write a java program to find factorial using recursive and non recursive
recursion is always slower than iteration
i love u darling
Pseudo code+factorial
int main (void) { puts ("18826771768889260997437677024916008575954036487149242588759823150835\ 31563316135988668829328894959231336464054459300577406301619193413805\ 97818883457558547055524326375565007131770880000000000000000000000000\ 000000"); return 0; }
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);
this is a code for calculating it recursivelly: float Factorial (float n) { if (n<=1) return 1.0; else return n* Factorial(n-1); }
by this program you can find the factorial: #include<iostream> using namespace std; main() { int n,x,f=1; cin>> n; x=0; while(x<n) { x++; f= f*x; } cout<<"factorial is"<<f<<"\n"; system("pause"); return 0; }
//program to find the factorial value f any number using while loop #include<stdio.h> void main() { int i,n,fact=1; printf("Enter the number\n"); scanf("%d",&n); i=n; while (i>=1) { fact=fact*i; i--; } printf("The factorial value=%d",fact); } the above is a program for calculating tha factorial value of any number which is entered by the user
I suggest to use a for loop, more or less like this (assuming the parameter is "n"): product = 1; for (int i = 1; i <= n; i++) { product *= i; }
#file.sh#run as sh file.shecho "Enter the no."read ni=nr=1while [ $i -ge 1 ]dor=`expr $r \* $i`i=`expr $i-1`doneecho Factorial is $rIf the ans helps you,plz increase the trust point.
== == using recursions: unsigned int Factorial( unsigned int x) { if(x>0) { return ( x * Factorial(x-1)); } else { return(1); } } factorial: unsigned int Factorial( unsigned int x) { unsigned int u32fact = 1; if( x == 0) { return(1); } else { while(x>0) { u32fact = u32fact *x; x--; } } }